2012-11-09 75 views
2

西納特拉,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

我認爲你應該重新考慮你的建模。恕我直言,它應該是書和用戶的機型,而且favorite_books,read_books和new_books都應該像這樣relationhips:

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 

我想這應該是一個更好的辦法。 =)

+0

Mongoid沒有has_many:通過 –

+0

嗯,不知道它。那麼,如何在mongoid上實現這種關係的關係呢? –

+0

也許它會有幫助http://stackoverflow.com/questions/7000605/how-to-implement-has-many-through-relationships-with-mongoid-and-mongodb –

相關問題