Mark,
Thank you for the input.
I already have the column names and values to search on - they were in
the params hash from my original sample. I get them when the user
submits the search form.
@params[:price_quote].each { | key, value | …
What I really would like to know how to do, though, is somehow
dynamically name the objects. For example, If I tell the function that
this is for the price_quote controller, then I want it to create the
proper pagination objects for me. Taking the sample from below, I want
to be able to name “@price_quote_pages” based on the input of the
function. It could be “@lead_pages”, “@activity_pages”, etc…
if @whereClause.length > 0 then
@price_quote_pages, @price_quotes = paginate :price_quote, :per_page =>
10, :conditions => @whereClause
…
The views rely on the name of these pagination objects to navigate
forward and backward through the records.
All in all, this search actually sucks pretty badly! It isn’t flexible
at all. I had a bug in my original post whereby I didn’t surround the
param values with single quotes (worked great for numeric fields
though)…hahaha. I just need something basic for simple search on
multiple fields without having to hard code the names (e.g.
ActiveRecord.find_all_by…and…and…) and get the results into a
pagination object!
Thanks,
Michael
Mark B. [email protected] wrote:
On Friday 11 November 2005 7:27 am, Michael wrote:
I would like to have a function called “getSearchResults” or something
similar where I pass in the name of the controller and the param hash.
Based on the input args, it would find the proper hash and create the page
objects with the controller name.
Any suggestions?
Hi Michael,
I’m quite new to Rails myself, but I’ll try to help.
Start by putting a function in app/controllers/application.rb:
private
def dynamic_search(
options = {
:controller_name => “default_controller”,
:search_params => {}
})
controller_name = options[:controller_name]
search_params = options[:search_params]
(your search code here)
end
Then in any of your app’s controllers you can call it using:
@results = dynamic_search( :controller_name =>
“some_controller”, :search_params => { :key1 => “value1”, :key2 =>
“value2”,
etc})
Using this technique, you could omit either of the parameters passed to
the
function by replacing “default_controller” with the name of, well, your
default controller, and adding code to your search routine to handle the
parameters. Here’s a hint:
for column in Price_quote.content_columns
…
end
Will let you iterate through the column names of the db table.
Good luck. (The more experienced of you please correct me where I’m
wrong)