Is there some seach algorithm available

excel_value = “Deal Bitte”
person_list = Person.people_of_project(session[:projectid]) %>

Person_list can have either the name deal or the name bitte
eg
person_list=
deal mueller
Barack Obama
Joachim Gauck

In this case I will have to default deal mueller

person_list=
Alex mueller
Barack Obama
Joachim Gauck
Text Bitte

In this case I will have to default Text Bitte

<%= select_tag(“person”,
options_from_collection_for_select(person_list,‘id’,‘full_name’
,default_person(excel_value,person_list) ))

def default_person(excel_value,person_list)
Is there some algorithm available
end

Hi,

So you’re looking for the name with “deal” or “bitte” in it?

Then you could do something like

person_list.each do |name|
return name if name.split.any? do |name_part|
[‘deal’, ‘bitte’].any? {|keyword| name_part.casecmp(keyword) == 0}
end
nil
end

This will split the name on spaces and check if any part is “deal” or
“bitte”, ignoring the letter case. If there’s any match, this name is
returned. If no name matches, nil is returned instead (you may also use
person_list.first or so).

The excel_value is a variable . It can contain 1 or 2 or 3 or 4 words
too

Jan E. wrote in post #1069310:

Hi,

So you’re looking for the name with “deal” or “bitte” in it?

Then you could do something like

person_list.each do |name|
return name if name.split.any? do |name_part|
[‘deal’, ‘bitte’].any? {|keyword| name_part.casecmp(keyword) == 0}
end
nil
end

This will split the name on spaces and check if any part is “deal” or
“bitte”, ignoring the letter case. If there’s any match, this name is
returned. If no name matches, nil is returned instead (you may also use
person_list.first or so).

def default_person(excel_value,person_list)
person_list.each do |name|
selected_name= name if name.split.any? do |name_part|
excel_value.split.any? {|keyword| name_part.casecmp(keyword) ==
0}
end
nil
end
# puts(selected_name)
return selected_name # i will send the id of the selected name
at the end
end

Attempt to call private method error
C:…/activerecord/lib/active_record/attribute_methods.rb:236:in
method_missing' C:.........:206:indefault_person’


deal bitte wrote in post #1069312:

The excel_value is a variable . It can contain 1 or 2 or 3 or 4 words
too

Well, then replace the fixed keywords with the keywords from
excel_value.

The “selected_name” variable is local to the block of the “each”
iterator. You have to define it outside the block:

def default_person(excel_value, person_list)
selected_name = nil
person_list.each do |name|

end
return selected_name
end

It’s probably even better to use Enumerable#find:

def default_person(excel_value, person_list)
person_list.find do |name|
name.split.any? do |name_part|

end
end
end

Thank you . Done