I would like to ask, what is a proper way to select books from given shelf
class Book < ActiveRecord::Base
belongs_to :shelf
end
class Shelf < ActiveRecord::Base
has_many :books
end
Using Book.where(:shelf => shelf) raises exception like no such column: book.shelf. I know I can reverse it, like shelf.books but this won’t work if there is need to filter by more than one object. All I could find is that query should looks like Books.where(:shelf_id => shelf.id) but this doesn’t seem very DRY or even a bit cool..
You can use joins or includes :
If you need precisions / have questions, don’t hesitate to comment 😉 (I’m thinking of you phrase “like shelf.books but this won’t work if there is need to filter by more than one object.” which I don’t really understand)