Trouble with the results of a dynamically generated find() method

Hi all,

So I have two tables: - apps (short for applicants)      has_and_belongs_to_many :locations

- locations      has_and_belongs_to_many :apps

In my system, a user searches for applicants by the locations to which they are associated, by using a dropdown menu that includes the names of locations (and sends to the method that returns the results the id of the selected location).

So, in the application for "joe," joe has indicated that he wants to live in "ethiopia" and "cambridge". If I search for all apps associated to "ethiopia," joe DOES result. That's good. But, in the result, in joe's row, only ethiopia appears. Where's cambridge?

Here's my find (which I've canned to look for ethiopia by it's id of 41): @apps = App.find(:all, :include => :locations, :conditions => ['locations.id = ?', 41])

Here's the result, in script/console: => [#<App:0x368bcf0 @locations=[#<Location:0x3689f2c @attributes={"status"=>"open", "name"=>"Ethiopia", "id"=>"41"}>], @attributes={"prefix"=>"Mr", "first_name"=>"joe", "last_name"=>"bloggs"}>]

So, my find is clearly limiting my result to show only the location that was searched for in joe's row. But I want to show joe's row complete with all his associations, including joe's association to cambridge. I just want to be able to search for joe by his association to 'ethiopia.'

Can anyone help me out?

Thanks, Brad

brad wrote:

Here's my find (which I've canned to look for ethiopia by it's id of 41): @apps = App.find(:all, :include => :locations, :conditions => ['locations.id = ?', 41])

Can anyone help me out?

Thanks, Brad

@apps = App.find(:all, :include => :locations, :conditions => ['locations.id in ?', [41, 54].join(',')])

hth

ilan

you can pass "true" as an argument to your association to force it to reload the values from the database.

For example:

App.find(:all, :conditions => ['locations.name = ?', 'Ethiopia'], :include => "locations").each do |user|   puts "user: #{user.name} num locations #{user.locations(true).length}" end

Mike