I am wondering what the easiest / most elegant way to select attributes from association models in has_many: through associations, is there.
Suppose we have items, catalogs, and CatalogItems with the following Item class:
class Item < ActiveRecord::Base
has_many :catalog_items
has_many :catalogs, :through => :catalog_items
end
Also, say that CatalogItems has a position attribute and that there is only one CatalogItem between any catalog and any item.
The most obvious, but slightly frustrating way to get the position attribute:
@item = Item.find(4)
@catalog = @item.catalogs.first
@cat_item = @item.catalog_items.first(:conditions => {:catalog_id => @catalog.id})
position = @cat_item.position
This is annoying because it seems that we should be able to do @ item.catalogs.first.position, since we have completely determined which position we want: the one that corresponds to the first of the @ item directories.
The only way I found this is:
class Item < ActiveRecord::Base
has_many :catalog_items
has_many :catalogs, :through => :catalog_items, :select => "catalogue_items.position, catalogs.*"
end
Item.catalogs.first.position. - . , @catalogs @item.catalogs. , - .
- ?
.