Hi, I am working on a project that needs a simple search feature but I have hit a roadblock. I need to design a search form that searches a lodging database for certain criteria such as lodge town and lodge amenities but my solution only partially works. My problem is with the Lodge.find method where I keep getting ActiveRecord::StatementInvalid errors. Because I am using a join table for the lodge amenities the find method can''t find a has_and_belongs_to_many relationship. I thought active record would be able to handle this relationship for me. Any help would be greatly appreciated. Here is my code: in lodges_controller: ------------------------------------------ def search @towns = Town.find(:all, :order => "name") @seasons = Season.find(:all) town_id = params[:town_id] seasons = :season_list @results = Lodge.find(:all, :conditions => ["town_id = ? and seasons = ?", town_id, seasons]) end in search.rhtml ------------------------------------------ <% form_tag( :action => :search ) do %> <p><strong>Search by Town:</strong> <%= select( ''town_id'', params[:town_id], @towns.collect{|t| [t.name, t.id]} ) %></p> <p><strong>Search by Season:</strong> <br \> <% for i in @seasons -%> <%= check_box_tag(''seasons_list[]'', params[i.id], checked = true) %> <%= i.season %> <br /> <% end -%> </p> <p><%= submit_tag( "Search" ) %></p> <% end %> --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
This might be a little better (warning, untested code ahead): in lodges_controller: ------------------------------------------ def search @towns = Town.find(:all, :order => "name") @seasons = Season.find(:all) @results = Lodge.find(:all, :conditions => { :town_id => params[:town_id], :seasons => params[:seasons_list] } ) end If my memory is correct, this means you will be able to pass nill, single values of arrays to each of the search values. You could then change your search form to include check boxes for the towns. Andrew Acroterra wrote:> Hi, I am working on a project that needs a simple search feature but I > have hit a roadblock. I need to design a search form that searches a > lodging database for certain criteria such as lodge town and lodge > amenities but my solution only partially works. My problem is with the > Lodge.find method where I keep getting ActiveRecord::StatementInvalid > errors. Because I am using a join table for the lodge amenities the > find method can''t find a has_and_belongs_to_many relationship. I > thought active record would be able to handle this relationship for > me. Any help would be greatly appreciated. Here is my code: > > in lodges_controller: > ------------------------------------------ > def search > @towns = Town.find(:all, :order => "name") > @seasons = Season.find(:all) > town_id = params[:town_id] > seasons = :season_list > > @results = Lodge.find(:all, :conditions => ["town_id = ? and > seasons = ?", town_id, seasons]) > end > > in search.rhtml > ------------------------------------------ > <% form_tag( :action => :search ) do %> > <p><strong>Search by Town:</strong> <%= select( ''town_id'', > params[:town_id], @towns.collect{|t| [t.name, t.id]} ) %></p> > <p><strong>Search by Season:</strong> <br \> > <% for i in @seasons -%> > <%= check_box_tag(''seasons_list[]'', params[i.id], checked = true) > %> <%= i.season %> <br /> > <% end -%> > </p> > <p><%= submit_tag( "Search" ) %></p> > <% end %>-- 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 -~----------~----~----~----~------~----~------~--~---
Using your code I am getting a NoMethodError - "undefined method for {:seasons=>:seasons_list, :town_id=>nil}:hash Any other suggestions? Thanks for you help. Wes On Jul 19, 6:15 am, Andrew Skegg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> This might be a little better (warning, untested code ahead): > > in lodges_controller: > ------------------------------------------ > def search > @towns = Town.find(:all, :order => "name") > @seasons = Season.find(:all) > @results = Lodge.find(:all, :conditions => { :town_id => > params[:town_id], > :seasons > => params[:seasons_list] } ) > end > > If my memory is correct, this means you will be able to pass nill, > single values of arrays to each of the search values. > You could then change your search form to include check boxes for the > towns. > > Andrew > > > > Acroterra wrote: > > Hi, I am working on a project that needs a simple search feature but I > > have hit a roadblock. I need to design a search form that searches a > > lodging database for certain criteria such as lodge town and lodge > > amenities but my solution only partially works. My problem is with the > > Lodge.find method where I keep getting ActiveRecord::StatementInvalid > > errors. Because I am using a join table for the lodge amenities the > > find method can''t find a has_and_belongs_to_many relationship. I > > thought active record would be able to handle this relationship for > > me. Any help would be greatly appreciated. Here is my code: > > > in lodges_controller: > > ------------------------------------------ > > def search > > @towns = Town.find(:all, :order => "name") > > @seasons = Season.find(:all) > > town_id = params[:town_id] > > seasons = :season_list > > > @results = Lodge.find(:all, :conditions => ["town_id = ? and > > seasons = ?", town_id, seasons]) > > end > > > in search.rhtml > > ------------------------------------------ > > <% form_tag( :action => :search ) do %> > > <p><strong>Search by Town:</strong> <%= select( ''town_id'', > > params[:town_id], @towns.collect{|t| [t.name, t.id]} ) %></p> > > <p><strong>Search by Season:</strong> <br \> > > <% for i in @seasons -%> > > <%= check_box_tag(''seasons_list[]'', params[i.id], checked = true) > > %> <%= i.season %> <br /> > > <% end -%> > > </p> > > <p><%= submit_tag( "Search" ) %></p> > > <% end %> > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Which version of rails are you running? The ability to use a hash for conditions was added in 1.2 (I think) On Jul 20, 2:16 am, Acroterra <acrote...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Using your code I am getting a NoMethodError - "undefined method for > {:seasons=>:seasons_list, :town_id=>nil}:hash > > Any other suggestions? Thanks for you help. > Wes > > On Jul 19, 6:15 am, Andrew Skegg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > This might be a little better (warning, untested code ahead): > > > in lodges_controller: > > ------------------------------------------ > > def search > > @towns = Town.find(:all, :order => "name") > > @seasons = Season.find(:all) > > @results = Lodge.find(:all, :conditions => { :town_id => > > params[:town_id], > > :seasons > > => params[:seasons_list] } ) > > end > > > If my memory is correct, this means you will be able to pass nill, > > single values of arrays to each of the search values. > > You could then change your search form to include check boxes for the > > towns. > > > Andrew > > > Acroterra wrote: > > > Hi, I am working on a project that needs a simple search feature but I > > > have hit a roadblock. I need to design a search form that searches a > > > lodging database for certain criteria such as lodge town and lodge > > > amenities but my solution only partially works. My problem is with the > > > Lodge.find method where I keep getting ActiveRecord::StatementInvalid > > > errors. Because I am using a join table for the lodge amenities the > > > find method can''t find a has_and_belongs_to_many relationship. I > > > thought active record would be able to handle this relationship for > > > me. Any help would be greatly appreciated. Here is my code: > > > > in lodges_controller: > > > ------------------------------------------ > > > def search > > > @towns = Town.find(:all, :order => "name") > > > @seasons = Season.find(:all) > > > town_id = params[:town_id] > > > seasons = :season_list > > > > @results = Lodge.find(:all, :conditions => ["town_id = ? and > > > seasons = ?", town_id, seasons]) > > > end > > > > in search.rhtml > > > ------------------------------------------ > > > <% form_tag( :action => :search ) do %> > > > <p><strong>Search by Town:</strong> <%= select( ''town_id'', > > > params[:town_id], @towns.collect{|t| [t.name, t.id]} ) %></p> > > > <p><strong>Search by Season:</strong> <br \> > > > <% for i in @seasons -%> > > > <%= check_box_tag(''seasons_list[]'', params[i.id], checked = true) > > > %> <%= i.season %> <br /> > > > <% end -%> > > > </p> > > > <p><%= submit_tag( "Search" ) %></p> > > > <% end %> > > > -- > > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---