Multiple uses of custom search function fails
helmerj opened this issue · 1 comments
Hi,
I am using meta_search to create an advanced search form in my rails 3.2.6 application.
I have two models with a has_many/belongs_to relationship:
The child models stores information from an external software application (matlab) in "name" - "value" pairs like this:
[name => "test", value => "0.7", parentmodel_id => "206" ]
[name => "intensity", value => "0.2", parentmodel_id => "206" ]
[name => "speed", value => "0.7", parentmodel_id => "206" ]
[name => "height", value => "1.0", parentmodel_id => "207" ]
I would like to loop over all existing child records and display their names as labels and then let the user search their specific values:
test: | from | | to |
intensity: | from | | to |
speed: | from | | to |
height: | from | | to |
I have created a custom search method using scopes:
scope :with_name_and_value_range,lambda {|name, low, high|
joins(:analytical_results).where(['analytical_results.name = ?', name]).with_low(low).with_high(high) }
scope :with_name,lambda {|name|
joins(:analytical_results).where(['analytical_results.name = ?', name])}
scope :with_low,lambda {|low|
joins(:analytical_results).where(['analytical_results.value >= ?', low]) unless low.blank?}
scope :with_high,lambda {|high|
joins(:analytical_results).where(['analytical_results.value <= ?', high]) unless high.blank?}
search_methods :with_name_and_value_range,
:splat_param => true, :type => [:string, :string, :string]
I then use the custom search in my view:
%dl.search
- @analytical_results.each_with_index do |ar, index|
%dt
= f.label :with_name_and_value_range, ar.name
%dd
= f.multiparameter_field :with_name_and_value_range,
{:field_type => :hidden_field, :value => ar.name },
{:field_type => :text_field},
{:field_type => :text_field},
:size => 3
The loop creates the search fields allright but the name and id of the created input fields are all the same for all of them "search[with_name_and_value_range(1)]". the counter goes obviously from 1 to 3 as I have one hidden field (1) and two input fields for low (2) and high (3).
When I force the id and name using an index in my loop resulting in a first three fields for test (1-3) and a second set of three (5-6). I end up with six arguments instead of just three and the search does not work anymore. I would need the array to be split (each_slice(3)) and then passed to the search method as a search query each.
Can this be controlled with the existing code? Can anyone point me to the file/function that I could modify/extend to get this additional functionality?
Thanks Juergen
I have solved the problem with a workaround of extracting the custom search method fields into a new hash using them to restrict the number of records and them merging them back.
Full description of the work around can be found here:
http://railsforum.com/viewtopic.php?pid=153712#p153712
Best Juergen