Sinatra,Mongoid 3
有4个模型:User, Book, FavoriteBooks, ReadBooks, NewBooks
。每个用户都有他们的收藏夹,阅读和新书的列表。一本书属于一个列表。但也可以要求提供有关任何书籍的信息,这些书籍应该不嵌入FavoriteBooks, ReadBooks, NewBooks
。
该计划的一部分:
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
好像我错过了什么。
如何让用户“包含”FavoriteBooks, ReadBooks, NewBooks
列表?我应该使用一对一的关系吗?
答案 0 :(得分:0)
我认为你应该重新考虑你的建模。恕我直言,它应该是书籍和用户作为模型,而favorite_books,read_books和new_books都应该是这样的关系:
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
我认为这应该是更好的方法。 =)