Problems with find

I don’t know the fundamental thing that I’m misunderstanding here about
the find().

In my action “view_style” i want to list the sizes related to the the
style (which is the id I’m looking at).

My tables are sizes, styles and products, where products is a join
table with id, size_id and style_id, so that size\ “has_many :styles,
:through => products” and style “has_many :sizes, :through => products”

So i need to write something along the functionality of (this
pseudocode)

@sizes = find ( size_ids where style_id == params[:id])

in order that i can loop through the sizes in a select box in the view,
but for whatever reason, everything that I’ve tried has totally failed.

I’m at a loss and I’ve been stalled on this problem for a while now.
Can anyone help?

Brandon Wright wrote:

I don’t know the fundamental thing that I’m misunderstanding here about
the find().

In my action “view_style” i want to list the sizes related to the the
style (which is the id I’m looking at).

My tables are sizes, styles and products, where products is a join
table with id, size_id and style_id, so that size\ “has_many :styles,
:through => products” and style “has_many :sizes, :through => products”

So i need to write something along the functionality of (this
pseudocode)

@sizes = find ( size_ids where style_id == params[:id])

in order that i can loop through the sizes in a select box in the view,
but for whatever reason, everything that I’ve tried has totally failed.

I’m at a loss and I’ve been stalled on this problem for a while now.
Can anyone help?

So I assume you have a @style variable in your controller. To create
your select you can simply iterate through @style.sizes

<% @style.sizes.each do |size| %>
<%= size.name %>
<% end %>

You can also use the option_for_select helper

<%= options_for_select(
@style.sizes.collect { |size|
[size.id, size.name]
}
) %>

It takes an array of [id, name] arrays to build you option tags for you.

options_for_select([[1, ‘foo’], [2, ‘bar’]])
#=> foo
bar