brad
2008-Feb-29 00:03 UTC
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ilan Berci
2008-Feb-29 02:20 UTC
Re: Trouble with the results of a dynamically generated find
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mike Garey
2008-Feb-29 09:45 UTC
Re: Trouble with the results of a dynamically generated find() method
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 On 2/28/08, brad <bradfordnoble-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---