Ruby Forum Ruby on Rails > Find conditions

Posted by Remco Zwaan (remco123)
on 08.05.2008 16:27
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..
Posted by Thorsten Mueller (thorsten)
on 08.05.2008 17:03
> 
>  @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)
Posted by Frederick Cheung (Guest)
on 08.05.2008 17:06
(Received via mailing list)
>  @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
Posted by Remco Zwaan (remco123)
on 08.05.2008 17:24
Frederick Cheung 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...
Posted by Thorsten Mueller (thorsten)
on 08.05.2008 17:26
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]
Posted by Remco Zwaan (remco123)
on 08.05.2008 17:35
Thorsten Mueller 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


Posted by Thorsten Mueller (thorsten)
on 08.05.2008 17:47
> 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
Posted by Remco Zwaan (remco123)
on 09.05.2008 08:36
Thorsten Mueller 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'
Posted by Remco Zwaan (remco123)
on 09.05.2008 09:20


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