Update Realtion

Hi I want to assign continents to my products with a drop down box. I managed to get everyting runing but now i have a probably quite easy to solve problem. I don't get the update running.

What want to do is basically: SELECT * FROM "continents" WHERE ("continents"."id" = "continent_id") LIMIT 1

But I don't find the right syntax.

At the moamnt it looks like this: SELECT * FROM "continents" WHERE (id = '--- :continend_id') LIMIT 1

The parameters of my update are looking like this: Parameters: {"commit"=>"Update", "action"=>"update", "_method"=>"put", "authenticity_token"=>"***", "product"=>{"image_url"=>"w.jpg", "expirience_points"=>"0", "price"=>"1", "title"=>"w", "description"=>"w", "continent_id"=>"7"}, "id"=>"11", "controller"=>"products"}

This is the code of may update methode: def update    @product = Product.find(params[:id]) @product.continent = Continent.find(:first, :conditions => [ "id = ?",:continend_id ])     if @product.update_attributes(params[:product])       flash[:notice] = 'Product was successfully updated.'       redirect_to(@product)     else       render :action => "edit"     end   end

Hi Ma We,

The parameters of my update are looking like this: Parameters: {"commit"=>"Update", "action"=>"update", "_method"=>"put", "authenticity_token"=>"***", "product"=>{"image_url"=>"w.jpg", "expirience_points"=>"0", "price"=>"1", "title"=>"w", "description"=>"w", "continent_id"=>"7"}, "id"=>"11", "controller"=>"products"}

This is the code of may update methode: def update @product = Product.find(params[:id]) @product.continent = Continent.find(:first, :conditions => [ "id = ?",:continend_id ])

You haven't posted what you're getting as a result, but one problem is that you don't have a variable named continend_id in your code. You have a params element named continent_id. Change the Continent.find clause to use :params[:continent_id]. A couple of other tips.

I always use .to_i on finds where I'm using a param since they're strings and find takes an integer. It helps to remind me that params are strings.

Your Continent.find could be shortened to Continent.find(params[:continent_id].to_i)

HTH, Bill

Thanks for your answer.

I tried already: :params[:continent_id] //Error: Couldn't find Continent with ID=0

the problem is that :continent_id is the foreign key in products and i'm looking for the :id in continents which is the same then :continent_id

I also tried to use: -:params[:continent_id].to_i //which comes out the same like :params[:continent_id] -:continent_id.to_i //Error: Couldn't find Continent with ID=103233 -:first, :conditions => [ "id = ?",:continend_id.to_i ]) //the same then above except that it don't brings the error because it just doesn't find ID=103233 and returns nil

The problem with :continent_id.to_i is that it is not casting the number value which should put in the case above 7 out.

- @product.continent = Continent.find(:first, :conditions => [ "id = ?",:continend_id ]) //returns nil the SQL looks like this SELECT * FROM "continents" WHERE (id = '--- :continend_id') LIMIT 1

Hi Marcus,

Thanks for your answer.

You're welcome.

-:first, :conditions => [ "id = ?",:continend_id.to_i ]) //the same then

Are you sure it's not just a typo?

continend_id vs continent_id

Best regards, Bill

You are right there was a typo but didn't change anything. to make sure I tried :id.to_i witch brings 3049 instead of 1

Markus Wellmann wrote:

You are right there was a typo but didn't change anything. to make sure I tried :id.to_i witch brings 3049 instead of 1

-:first, :conditions => [ "id = ?",:continend_id.to_i ]) //the same then

You're trying to pass a symbol instead of a variable into your query string, so of course it won't work. What you want is ["id = ?", continent_id.to_i] (without the colon).

But you should never need to do it this way. For a simple equality check, use the hash syntax: {:id => continent_id.to_i} or consider find or find_by_id .

Are you sure it's not just a typo?

continend_id vs continent_id

Best,

Marnen Laibow-Koser wrote:

You're trying to pass a symbol instead of a variable into your query string, so of course it won't work. What you want is ["id = ?", continent_id.to_i] (without the colon).

But you should never need to do it this way. For a simple equality check, use the hash syntax: {:id => continent_id.to_i} or consider find or find_by_id .

Ok I do understand now the Problem with the Symbols. But with @product.continent = Continent.find({:id => continent_id.to_i}) I get undefined local variable or method `continent_id' waht I do understand because I'm in def update and the parameters witch are passed are:

Parameters: {"commit"=>"Upd.... , "product"=>{"image_url"=>"/images/Burger.png", "expirience_points"=>"10", "price"=>"1.99", "title"=>"WHOPPER®", "description"=>"....", "continent_id"=>"7"}, "id"=>"1", "controller"=>"products"}

But how do I get now from :product the "continent_id"=>"7"

The answer is Continent.find(params[:product]["continent_id"])

in more Steps

first=params[:product] second=first["continent_id"] Continent.find(secon)

May be there is a better way as well but any way it works finally.

thanks to for Helping to get on the right way!

Markus Wellmann wrote: in def update and the parameters witch