Pass in the product_id to the partial, without doing eager loading of the
items.
@products = Product.find(:all, :order => "description")
Then do a
@product = Product.find(params[:id])
@product.items
to get the items in the partial.
On Jan 8, 2008 9:11 PM, hiddenhippo
<redaudi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> Hi, hoping someone can help, or at least suggest a better way of
> doing the following.
>
> I have two models which have a has_and_belongs_to_many relationship.
> for example products and items. On my page view I provide a selection
> list (collection_select) of the products, and when the user changes
> the selection I fetch the items that are relevant to that product.
> However as part of optimising the code I''ve been using the
following
> when assigning the products variable
>
> @products > Product.find(:all,
:include=>[:items],:order=>"products.description")
>
> I then bind the product dropdown with @products, and subject to what
> selection they make I need to pass the correct items to an item
> partial. I know this has to be done by effectively passing
> @products[0].items, but how do I figure out where in the array my
> original product is, e.g. :products[0] or @products[1]... at the
> moment I can only think of looping over the @products array looking
> for a matching id and when I''ve got that pass the items and break
out
> of the loop. I''m pretty certain there must be a cleaner way
rather
> than manually coding the loop?
>
> Any ideas or suggestions would be greatly appreciated.
>
> thanks
> >
>
--
Ryan Bigg
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---