2

辛纳特拉,蒙古人 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?我应该使用一对一的关系吗?

4

1 回答 1

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. =)

于 2012-11-09T15:09:17.957 回答