2

Sinatra, Mongoid 3

There 4 models: User, Book, FavoriteBooks, ReadBooks, NewBooks. Each user has their list of the favourites, read and new books. A book belongs to a list. But it's also possible to request an information about any book which means books should not be embedded into FavoriteBooks, ReadBooks, NewBooks.

The part of the scheme:

class Book
 include Mongoid::Document
 belongs_to :favourite_books
 belongs_to :read_books
 belongs_to :new_books 
end

class FavoriteBook
 include Mongoid::Document
 has_many :books
end

#.... the same for ReadBooks and NewBooks


class User
 include Mongoid::Document
 # what else?
end

It seems like I missed something.

What should I do to make a user "contain" the lists of FavoriteBooks, ReadBooks, NewBooks? Should I use one-to-one relationship?

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
Alan Coromano
  • 24,958
  • 53
  • 135
  • 205

1 Answers1

0

I think you should rethink your modeling. IMHO it should be book and user as models, and the favorite_books, read_books and new_books should all be relationhips like so:

class User
 include Mongoid::Document
 has_many :favorite_books
 has_many :read_books
 has_many :new_books

 has_many :books, :through => :favorite_books
 has_many :books, :through => :read_books
 has_many :books, :through => :new_books
end

class Book
 include Mongoid::Document
 has_many :favorite_books
 has_many :read_books
 has_many :new_books

 has_many :users, :through => :favorite_books
 has_many :users, :through => :read_books
 has_many :users, :through => :new_books 
end

class FavoriteBook
 include Mongoid::Document
 belongs_to :books
 belongs_to :users
end

#.... the same for ReadBooks and NewBooks

I think this should be a better approach. =)

Paulo Henrique
  • 1,025
  • 8
  • 12