# Using a variable to invoke a custom method within autocomple

**URL:** <https://www.ruby-forum.com/t/using-a-variable-to-invoke-a-custom-method-within-autocomple/85755>\
**Category:** Rails\
**Created:** [February 16, 2007, 2:22am UTC](https://www.ruby-forum.com/t/using-a-variable-to-invoke-a-custom-method-within-autocomple/85755 "2007-02-16T02:22:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dimmerswitch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@Dimmerswitch](https://www.ruby-forum.com/u/Dimmerswitch)\
**Post date:** [February 16, 2007, 2:22am UTC](https://www.ruby-forum.com/t/using-a-variable-to-invoke-a-custom-method-within-autocomple/85755/1 "2007-02-16T02:22:39Z")

</div>

I’m implementing a version of the autocompletion script found at  
[http://ricardo.pacheco.name/blog/articles/2006/07/26/using-](http://ricardo.pacheco.name/blog/articles/2006/07/26/using-)  
autocomplete-with-belongs\_to-fields and have run into a stumbling  
block.

The issue involves passing in a variable to indicate which custom  
method should be invoked. I have an ugly workaround (a duplicate  
function hardcoded to use the custom method), but would like to learn  
a better way to handle this case. It feels like I’m missing something  
simple, but I’ll be damned if I can see it.

# from: app/helpers/application\_helper.rb

def indexed\_auto\_complete\_result(entries, entityType, field, index)  
return unless entries  
items = entries.map { |entry| content\_tag(“li”, entry[field], “id”  
=\> entityType+‘::’+entry[index].to\_s) }  
content\_tag(“ul”, items.uniq)  
end

This works fine for referencing model attributes which exist in the  
“Highlights” table (name, description, etc.) but fails if ‘field’ is  
passed in as “name\_with\_place”. The unordered list is created, but  
it’s full of empty

- tags. If I change it to use entry.field I get  
NoMethodError (undefined method ‘field’) errors in the log.

My eventual workaround was to add this function for those times I need  
to access the name\_with\_place method. This works as desired, and  
properly invokes the custom name\_with\_place method. It’s clearly not  
DRY, though.

def indexed\_auto\_complete\_result\_with\_place(entries, entityType,  
field, index)  
return unless entries  
items = entries.map { |entry| content\_tag(“li”,  
entry.name\_with\_place, “id” =\> entityType+‘::’+entry[index].to\_s)}  
content\_tag(“ul”, items.uniq)  
end

# from: app/models/highlight.rb

belongs\_to :place

def name\_with\_place  
if self.place.nil?  
self.name  
else  
“#{self.name} (#{self.place.name})”  
end  
end

# from: app/controllers/highlights\_controller.rb

def auto\_complete\_for\_highlight\_internal\_name  
find\_options = {  
:conditions =\> [“LOWER(name) LIKE ?”, ‘%’ +params[:highlight]  
[:internal\_name].downcase + ‘%’ ],  
:order =\> “name ASC”,  
:limit =\> 10  
}  
@highlights = Highlight.find(:all,find\_options)  
render :inline =\> “\<%= indexed\_auto\_complete\_result @highlights,  
‘highlight\_id’, ‘name\_with\_place’, ‘id’ %\>”  
end

# from: public/javascripts/application.js

function auto\_complete\_on\_select(element, selectedElement)  
{  
var entityParts = selectedElement.id.split(‘::’);  
var entityType = entityParts[0];  
var entityId = entityParts[1];  
document.getElementById(entityType).value = entityId;  
}

# from: app/views/highlights/index.rhtml

\<% form\_tag(‘/highlights/edit’, :method =\> :post) do %\>  
\<%= text\_field\_with\_auto\_complete :highlight, :internal\_name, { :size  
=\> 40 }, { :after\_update\_element =\>  
‘auto\_complete\_and\_redirect\_on\_select’ } %\>  
\<%= hidden\_field ‘highlight’, ‘id’ %\>  
\<%= submit\_tag(“Edit”) %\>  
\<% end -%\>

---

<div class="post-metadata">

**Author:** ![Dimmerswitch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@Dimmerswitch](https://www.ruby-forum.com/u/Dimmerswitch)\
**Post date:** [February 16, 2007, 2:48am UTC](https://www.ruby-forum.com/t/using-a-variable-to-invoke-a-custom-method-within-autocomple/85755/2 "2007-02-16T02:48:05Z")

</div>

Not sure if this is what you want, but object.send(function\_name) will  
do the trick for cases like that. Just be careful since I think .send  
will execute any function, public or private.

---

<div class="post-metadata">

**Author:** ![Dimmerswitch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@Dimmerswitch](https://www.ruby-forum.com/u/Dimmerswitch)\
**Post date:** [February 16, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/using-a-variable-to-invoke-a-custom-method-within-autocomple/85755/3 "2007-02-16T14:57:46Z")

</div>

That works great for the dropdown. I’ll take a look at .send and the  
potential for executing any object method, which might make this  
approach a nonstarter.

Thanks, Jon
