# Update Realtion

**URL:** <https://www.ruby-forum.com/t/update-realtion/189822>\
**Category:** Rails\
**Created:** [June 8, 2010, 12:23pm UTC](https://www.ruby-forum.com/t/update-realtion/189822 "2010-06-08T12:23:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrwellmann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@mrwellmann](https://www.ruby-forum.com/u/mrwellmann)\
**Post date:** [June 8, 2010, 12:23pm UTC](https://www.ruby-forum.com/t/update-realtion/189822/1 "2010-06-08T12:23:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mrwellmann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@mrwellmann](https://www.ruby-forum.com/u/mrwellmann)\
**Post date:** [June 8, 2010, 12:58pm UTC](https://www.ruby-forum.com/t/update-realtion/189822/2 "2010-06-08T12:58:05Z")

</div>

Hi Ma We,

On Tue, Jun 8, 2010 at 5:23 AM, Ma We [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @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

---

<div class="post-metadata">

**Author:** ![mrwellmann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@mrwellmann](https://www.ruby-forum.com/u/mrwellmann)\
**Post date:** [June 8, 2010, 1:39pm UTC](https://www.ruby-forum.com/t/update-realtion/189822/3 "2010-06-08T13:39:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mrwellmann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@mrwellmann](https://www.ruby-forum.com/u/mrwellmann)\
**Post date:** [June 8, 2010, 1:48pm UTC](https://www.ruby-forum.com/t/update-realtion/189822/4 "2010-06-08T13:48:27Z")

</div>

Hi Marcus,

On Tue, Jun 8, 2010 at 6:39 AM, Markus W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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

---

<div class="post-metadata">

**Author:** ![mrwellmann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@mrwellmann](https://www.ruby-forum.com/u/mrwellmann)\
**Post date:** [June 8, 2010, 2:12pm UTC](https://www.ruby-forum.com/t/update-realtion/189822/5 "2010-06-08T14:12:50Z")

</div>

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
> 
> Are you sure it’s not just a typo?
> 
> continend\_id vs continent\_id

---

<div class="post-metadata">

**Author:** ![mrwellmann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@mrwellmann](https://www.ruby-forum.com/u/mrwellmann)\
**Post date:** [June 8, 2010, 3:22pm UTC](https://www.ruby-forum.com/t/update-realtion/189822/6 "2010-06-08T15:22:23Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![mrwellmann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@mrwellmann](https://www.ruby-forum.com/u/mrwellmann)\
**Post date:** [June 8, 2010, 2:38pm UTC](https://www.ruby-forum.com/t/update-realtion/189822/7 "2010-06-08T14:38:17Z")

</div>

Markus W. 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  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mrwellmann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/848f3c/32.png) [@mrwellmann](https://www.ruby-forum.com/u/mrwellmann)\
**Post date:** [June 8, 2010, 4:32pm UTC](https://www.ruby-forum.com/t/update-realtion/189822/8 "2010-06-08T16:32:41Z")

</div>

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 W. wrote:  
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”
