James Bond wrote:
> There is example:
> http://wiki.rubyonrails.org/rails/pages/ThroughAssociations
>
> Example
>
> Catalogue has_many :catalogue_items
> CatalogueItems belongs_to :catalogue; belongs_to :product
> Product has_many :catalogue_items
>
> So how I can get:
>
> catalogues.name
> catalogue_items.position
> products.name
> products. price
> WHERE
> catalogues.name LIKE "xxx%"
First of all, amend your association in Catalogue as follows - this will
mean that the catalogue's items (and therefore products) will
automatically be ordered by position.
Catalogue
has_many :catalogue_items, :order => "position"
Then, in the controller:
#do an include to eager load the relevant associations.
@catalogues = Catalogue.find(:all, :conditions => ["name like ?",
"#{a_variable}%"], :include => {:catalogue_items => :products})
And then, for example, in the view:
<% @catalogues.each do |catalogue| %>
<div>Catalogue:<%= catalogue.name %></br/>
<% catalogue.catalogue_items.each do |item| %>
<div class="product"><%= item.position %>: <%= item.product.name
%>: <%= item.product.price %></div>
<% end %>
</div>
<% end %>
Obviously you can put your own html around this, this is just a simple
example.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---