# Find conditions

**URL:** <https://www.ruby-forum.com/t/find-conditions/137054>\
**Category:** Rails\
**Created:** [May 8, 2008, 4:27pm UTC](https://www.ruby-forum.com/t/find-conditions/137054 "2008-05-08T16:27:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [May 8, 2008, 4:27pm UTC](https://www.ruby-forum.com/t/find-conditions/137054/1 "2008-05-08T16:27:03Z")

</div>

This are my models:

class Werelddeel \< ActiveRecord::Base  
has\_many :land, :foreign\_key =\> “werelddeel\_id”  
set\_table\_name “werelddelen”  
set\_primary\_key “werelddeel\_id”  
has\_permalink :zoeknaam

def to\_param  
zoeknaam  
end

end

class Land \< ActiveRecord::Base  
belongs\_to :werelddeel, :foreign\_key =\> “werelddeel\_id”  
set\_table\_name “landen”  
set\_primary\_key “landcode”

has\_permalink :land\_zoeknaam

def to\_param  
land\_zoeknaam  
end  
end

I want to find al the (land)countries belongs to the  
(werelddeel)continent.

@landen = Land.find(:all, :conditions =\> { :werelddeel\_id =\>  
‘#{werelddeel\_id}’ })

This code does nothing…what i am doing wrong??

Grtz…

---

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [May 8, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/find-conditions/137054/2 "2008-05-08T17:03:02Z")

</div>

> @landen = Land.find(:all, :conditions =\> { :werelddeel\_id =\>  
> ‘#{werelddeel\_id}’ })

‘#{werelddeel\_id}’  
must be:  
“#{werelddeel\_id}”  
the #{} thingy works only in double quotes

in case of such errors have a look in log/development.log too see how  
the generated sql looks like. it will contain something like “…WHERE  
werelddeel\_id = #{werelddeel\_id}…”

by the way if you already have the continent, eg in @weerelddel you can  
just write:  
@weerelddel.land  
or  
@weerelddel.landen  
(not sure how this works with your naming)

---

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [May 8, 2008, 5:06pm UTC](https://www.ruby-forum.com/t/find-conditions/137054/3 "2008-05-08T17:06:33Z")

</div>

> @landen = Land.find(:all, :conditions =\> { :werelddeel\_id =\>  
> ‘#{werelddeel\_id}’ })
> 
> This code does nothing…what i am doing wrong??

By does nothing I assume you mean that an empty array is returned. A  
string literal with single quotes isn’t interpolated, so the query  
actually says WHERE wereldeel\_id = ‘#{werelddeel\_id}’ (which obviously  
matches nothing). You need double quotes for interpolation to happen,  
although it’s not clear to me why you can’t just do :werelddeel\_id =\>  
werelddeel\_id or even easier:  
Land.find\_all\_by\_werelddeel\_id(werelddeel\_id)

Fred

---

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [May 8, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/find-conditions/137054/4 "2008-05-08T17:24:18Z")

</div>

Frederick C. wrote:

> > ï¿½@landen = Land.find(:all, :conditions =\> { :werelddeel\_id ï¿½=\>  
> > ‘#{werelddeel\_id}’ })
> > 
> > This code does nothing…what i am doing wrong??
> 
> By does nothing I assume you mean that an empty array is returned. A  
> string literal with single quotes isn’t interpolated, so the query  
> actually says WHERE wereldeel\_id = ‘#{werelddeel\_id}’ (which obviously  
> matches nothing). You need double quotes for interpolation to happen,  
> although it’s not clear to me why you can’t just do :werelddeel\_id =\>  
> werelddeel\_id or even easier:  
> Land.find\_all\_by\_werelddeel\_id(werelddeel\_id)
> 
> Fred

I just tried

@landen = Land.find(:all, :conditions =\> { :werelddeel\_id =\>  
“#{werelddeel\_id}” })  
@landen = Land.find\_all\_by\_werelddeel\_id(werelddeel\_id)

i get the error message “undefined local variable or method  
`werelddeel\_id’”

if i do static search like this:

@landen = Land.find(:all, :conditions =\> { :werelddeel\_id =\> 1 })

1 = Afrika

I works…so the relationship works well…(i guess)

But this is not what i want…

Grtz…

---

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [May 8, 2008, 5:26pm UTC](https://www.ruby-forum.com/t/find-conditions/137054/5 "2008-05-08T17:26:11Z")

</div>

then you didn’t set werelddeel\_id before using it.

if you expect it as param in an url link then it would be:

params[:werelddeel\_id]

---

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [May 8, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/find-conditions/137054/6 "2008-05-08T17:35:54Z")

</div>

Thorsten M. wrote:

> then you didn’t set werelddeel\_id before using it.
> 
> if you expect it as param in an url link then it would be:
> 
> params[:werelddeel\_id]

i tried this.

Werelddeel controller  
def show  
@werelddeel = Werelddeel.find(params[:werelddeel\_id])  
@landen = Land.find\_all\_by\_werelddeel\_id(werelddeel\_id)  
end

errormessage  
Couldn’t find Werelddeel without an ID

---

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [May 9, 2008, 8:36am UTC](https://www.ruby-forum.com/t/find-conditions/137054/7 "2008-05-09T08:36:08Z")

</div>

Thorsten M. wrote:

> > Werelddeel controller  
> > def show  
> > @werelddeel = Werelddeel.find(params[:werelddeel\_id])  
> > @landen = Land.find\_all\_by\_werelddeel\_id(werelddeel\_id)  
> > end
> > 
> > errormessage  
> > Couldn’t find Werelddeel without an ID
> 
> ok, the default parameter for the show action is named id.
> 
> def show  
> @werelddeel = Werelddeel.find(params[:id])  
> @landen = Land.find\_all\_by\_werelddeel\_id(@werelddeel.id)  
> end
> 
> should work then
> 
> but
> 
> def show  
> @werelddeel = Werelddeel.find(params[:id])  
> @landen = @werelddeel.landen # or .land, (?) with your naming  
> end
> 
> read the development.log  
> this shows you lots of details for all calls your app makes  
> especially which parameters are sent to it. clear the file in a  
> texteditor,  
> save the empty file, run your app and have a look at it. this will help  
> you a lot finding detail bugs like that.  
> or just have a look at the output of your server, that’s just the same  
> as in the logfile
> 
> you’ll get some output like:
> 
> Processing ProductsController#show (for 127.0.0.1 at 2008-05-08  
> 17:45:21) [GET]  
> Parameters: {“action”=\>“show”, “id”=\>“670060391”,  
> “controller”=\>“member/products”, “page”=\>“1”}
> 
> (shortened)  
> you can see which controller/action was called, which method used  
> and most important get a list with the parameters

i get this from my logfile:

Processing WerelddeelController#show (for 92.64.217.3 at 2008-05-09  
08:34:32) [GET]  
Session ID:  
BAh7ByIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%0ASGFzaHsABjoKQHVzZWR7ADoMY3NyZl9pZCIlMzBkMGExNWFkYzgyMGU1NDgz%0AZWE5OGYwM2MyYjU5N2M%3D–0b8fff2e25fb9af485a51e8e99ab7347882152aa  
Parameters: {“action”=\>“show”, “id”=\>“europa”,  
“controller”=\>“werelddeel”}

NameError (undefined local variable or method `werelddeel_id' for #<WerelddeelController:0xb7735aac>): /app/controllers/werelddeel_controller.rb:13:in`show’

---

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [May 9, 2008, 9:20am UTC](https://www.ruby-forum.com/t/find-conditions/137054/8 "2008-05-09T09:20:16Z")

</div>

He guys…

It works…

@werelddeel = Werelddeel.find\_by\_zoeknaam(params[:id])  
@landen = Land.find\_all\_by\_werelddeel\_id(@werelddeel.werelddeel\_id)

Thanks to you all…for you’re support!!

---

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [May 8, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/find-conditions/137054/9 "2008-05-08T17:47:39Z")

</div>

> Werelddeel controller  
> def show  
> @werelddeel = Werelddeel.find(params[:werelddeel\_id])  
> @landen = Land.find\_all\_by\_werelddeel\_id(werelddeel\_id)  
> end
> 
> errormessage  
> Couldn’t find Werelddeel without an ID

ok, the default parameter for the show action is named id.

def show  
@werelddeel = Werelddeel.find(params[:id])  
@landen = Land.find\_all\_by\_werelddeel\_id(@werelddeel.id)  
end

should work then

but

def show  
@werelddeel = Werelddeel.find(params[:id])  
@landen = @werelddeel.landen # or .land, (?) with your naming  
end

read the development.log  
this shows you lots of details for all calls your app makes  
especially which parameters are sent to it. clear the file in a  
texteditor,  
save the empty file, run your app and have a look at it. this will help  
you a lot finding detail bugs like that.  
or just have a look at the output of your server, that’s just the same  
as in the logfile

you’ll get some output like:

Processing ProductsController#show (for 127.0.0.1 at 2008-05-08  
17:45:21) [GET]  
Parameters: {“action”=\>“show”, “id”=\>“670060391”,  
“controller”=\>“member/products”, “page”=\>“1”}

(shortened)  
you can see which controller/action was called, which method used  
and most important get a list with the parameters
