User Favorites

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:

end
The table favorite just has a two fields : user_id, book_id
http://lists.rubyonrails.org/mailman/listinfo/rails
Add 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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs