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