Hi,
I''m having a simple problem with my usage of Rails and was wondering
what
was the most elegant way to solve it (I don''t like much the way I di
it). I
have a Song model class that has a belongs_to relationship to a Band class.
When I create the song, I just want to give the artist at the same time from
a combo box filled up with all existing artists. Quite simple.
So in my ProductsController :
def new
@artists = Artist.find(:all)
@product = Product.new
end
And in my product form :
<p><label
for="product_price">Artist</label><br/>
<%= select ''product'', ''artist'',
@artists.collect{ |a| [a.name<http://a.name>,
a.id <http://a.id>] } %></p>
But then when I click the create button and the create method gets executed
I get an error when a new song is created:
def create
@product = Product.new(params[:product])
...
end
As here the artist is a String containing and id and not an Artist instance.
Fair enough. So I was wondering what was the most elegant way of
''replacing''
the id with the actual Artist instance (loaded with a find). Right now
I''m
just executing the following code before the product creation:
if params[:product][:artist].instance_of? String
params[:product][:artist] = Artist.find(params[:product][:artist])
end
But this is rather ugly. I could also redefine the Product initializer but I
like to rely on the ActiveRecord''s one that fills up all the attributes
for
me.
Any idea or suggestion? Thanks a lot for your help.
Matthieu Riou.
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
> <p><label for="product_price">Artist</label><br/> > <%= select ''product'', ''artist'', @artists.collect{ |a| [a.name, a.id] } > %></p><p><label for="product_price">Artist</label><br/> <%= select ''product'', ''artist_id'', @artists.collect{ |a| [a.name, a.id] } %></p>