Hi there, I''m currently creating a simple (at least so I thought) structure of user favorites. Basically users can add certain ''objects'' to their list of favorites. Here is my current structure (simplified) class User has_many :favorites end class Book belongs_to :favorites end class Favorite belongs_to :user has_one :book end The table favorite just has a two fields : user_id, book_id Is this how you would do it? What method would you have in User to get a list of favorite books? Thanks for reading and any thoughts you may have of this! Neil
On Friday, August 11, 2006, at 8:07 PM, Neil Edwards wrote:>Hi there, > >I''m currently creating a simple (at least so I thought) structure of >user favorites. Basically users can add certain ''objects'' to their >list of favorites. > >Here is my current structure (simplified) > >class User >has_many :favorites >end > >class Book >belongs_to :favorites >end > >class Favorite >belongs_to :user >has_one :book >end > >The table favorite just has a two fields : user_id, book_id > >Is this how you would do it? What method would you have in User to get >a list of favorite books? > >Thanks for reading and any thoughts you may have of this! > >Neil >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsAdd a has_many :books, :through=>:favorites to your User class, then you can do this.. user.books to get the favorite ones. _Kevin www.sciwerks.com -- Posted with http://DevLists.com. Sign up and save your mailbox.
class User has_many :favorites has_many :favorite_books, :through => :favorites, :class_name => ''books'' end class Book has_many :favorites has_many :favorites_users, :through => :favorites, :class_name => ''users'' end class Favorites belongs_to :user belongs_to :book end you access favorite books with user.favorite_books you acess users who ''favorited'' a book with book.favorites_users On 8/11/06, Neil Edwards <neil@plasticwater.com> wrote:> > Hi there, > > I''m currently creating a simple (at least so I thought) structure of > user favorites. Basically users can add certain ''objects'' to their > list of favorites. > > Here is my current structure (simplified) > > class User > has_many :favorites > end > > class Book > belongs_to :favorites > end > > class Favorite > belongs_to :user > has_one :book > end > > The table favorite just has a two fields : user_id, book_id > > Is this how you would do it? What method would you have in User to get > a list of favorite books? > > Thanks for reading and any thoughts you may have of this! > > Neil > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- ______________ Heri R. http://sprinj.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060814/bfc98aae/attachment.html