Modeling A Book With Ruby on Rails Active Record
Im modeling an online book with ruby on rails. The book has chapters, each chapter has pages. Modeling this part is easy. What id like 开发者_Python百科to do is keep track of what pages each user has read and when and whether they liked that page / whether the user completed the book. How would one recommend modeling keeping track of this in rails? Id ideally like to say give me all the books the user is or has read, and then have access to the book plus state info for that user's reading of the book.
The BookState becomes the intermediate object between the user and the book.
class User < ActiveRecord::Base
has_many :book_states
has_many :books, :through => :book_state
end
class BookState < ActiveRecord::Base
belongs_to :user
belongs_to :book
end
class Book < ActiveRecord::Base
has_many :book_states
end
BookState has the current location, the likes/dislikes, etc. If you ask for a user's bookStates, that will give you the books as well.
This makes a lot of sense. The one problem i see is collecting stats on a question (what percent of the time was this answered)? Since that data is in serialized hashes? What about this:
book
has_many user_books
has_many users through user_books
has_many chapters
chapter
belongs_to book
has_many pages
page
belongs_to chapter
has_many user_pages
user
has_many user_books
has_many books through user_books
user_book
belongs_to user
belongs_to book
has_many user_pages
user_page
belongs_to user_book
belongs_to page
What do you think?
精彩评论