Forum: Ruby on Rails What does the send() method do on a model object?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Wes G. (Guest)
on 2006-03-14 19:54
All,

I did a scaffold for a model and I ended up with this code in
list.rhtml:

 <% for column in TargetList.content_columns %>
    <td><%=h target_list.send(column.name) %></td>
  <% end %>

Where is the "send" method and what does it do?

I can't find it on ActiveRecord::Base?   I assume that it returns a
value based on the column id but I can't find any documentation on it.

Thanks,
Wes
unknown (Guest)
on 2006-03-14 20:00
(Received via mailing list)
Hi --

On Tue, 14 Mar 2006, Wes G. wrote:

>
> I can't find it on ActiveRecord::Base?   I assume that it returns a
> value based on the column id but I can't find any documentation on it.

send sends a message to an object.  In this case, you're sending the
name of the column to the object in target_list.

Sending a message is basically equivalent to calling a method.  But
since you don't know the name(s) of the methods in advance (they're in
the columns), you use send to generalize the process.

It's a built-in Ruby facility; it's used in many programs and systems,
not just Rails.


David

--
David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
Ben Sinclair (Guest)
on 2006-03-14 20:00
Send is a method from Object that calls the method you specify. That
code looks weird to me, but I haven't played with scaffolding in a
while.

In normal use, you would probably just call the method directly. In this
case the method is just the field you want to output.

Wes G. wrote:
> All,
>
> I did a scaffold for a model and I ended up with this code in
> list.rhtml:
>
>  <% for column in TargetList.content_columns %>
>     <td><%=h target_list.send(column.name) %></td>
>   <% end %>
>
> Where is the "send" method and what does it do?
>
> I can't find it on ActiveRecord::Base?   I assume that it returns a
> value based on the column id but I can't find any documentation on it.
>
> Thanks,
> Wes
Wes G. (Guest)
on 2006-03-14 20:02
Of course, I didn't go _all_ the way up the inheritance hierarchy :).

I was kind of hoping it was a mixin so I could learn more about them -
all in due time, I'm sure.

Thanks,
Wes

Ben Sinclair wrote:
> Send is a method from Object that calls the method you specify. That
> code looks weird to me, but I haven't played with scaffolding in a
> while.
>
> In normal use, you would probably just call the method directly. In this
> case the method is just the field you want to output.
>
> Wes G. wrote:
>> All,
>>
>> I did a scaffold for a model and I ended up with this code in
>> list.rhtml:
>>
>>  <% for column in TargetList.content_columns %>
>>     <td><%=h target_list.send(column.name) %></td>
>>   <% end %>
>>
>> Where is the "send" method and what does it do?
>>
>> I can't find it on ActiveRecord::Base?   I assume that it returns a
>> value based on the column id but I can't find any documentation on it.
>>
>> Thanks,
>> Wes
Dylan S. (Guest)
on 2006-03-14 20:09
(Received via mailing list)
Couldn't you also do something like this ?

<%=h target_list.#{column.name} %>

Untested, but I remember having to call a few methods without knowing
their
names first, and used something similar to this... however... send is
probably more aesthetic than any hack I came up with :)
This topic is locked and can not be replied to.