Find conditions

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

@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

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

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]

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

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

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'

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