Auto_complete_result field rather than method?

I just noticed that the auto_complete_result method in the
auto_complete plugin calls the field (as opposed to the method) to
generate the result list.

Here’s what it is:

def auto_complete_result(entries, field, phrase = nil)
return unless entries
items = entries.map { |entry| content_tag(“li”, phrase ?
highlight(entry[field], phrase) : h(entry[field)) }
content_tag(“ul”, items.uniq)
end

I’ve changed it to (and what I think it should to be) :

def auto_complete_result(entries, method, phrase = nil)
return unless entries
items = entries.map { |entry| content_tag(“li”, phrase ?
highlight(entry.send(method), phrase) : h(entry.send(method))) }
content_tag(“ul”, items.uniq)
end

What is the rationale for using the field rather than the method?

Thanks for the tip!
I totally agree with you that the method makes more sense (and allows
for more flexibility) and I have implemented the same change in my
application based on your code.

My person model has first_name and last_name fields so to make a list of
person results, I would have to choose between displaying either first
name or last name in the auto-complete result list, which isn’t
acceptable. So I created a method full_name that combines the two fields
and after implementing your changes, I am able to populate the result
list using the full_name method call instead of one of the fields.

tekwiz wrote:

I just noticed that the auto_complete_result method in the
auto_complete plugin calls the field (as opposed to the method) to
generate the result list.

Here’s what it is:

def auto_complete_result(entries, field, phrase = nil)
return unless entries
items = entries.map { |entry| content_tag(“li”, phrase ?
highlight(entry[field], phrase) : h(entry[field)) }
content_tag(“ul”, items.uniq)
end

I’ve changed it to (and what I think it should to be) :

def auto_complete_result(entries, method, phrase = nil)
return unless entries
items = entries.map { |entry| content_tag(“li”, phrase ?
highlight(entry.send(method), phrase) : h(entry.send(method))) }
content_tag(“ul”, items.uniq)
end

What is the rationale for using the field rather than the method?