Wes Gamble
2006-Mar-14 17:54 UTC
[Rails] What does the send() method do on a model object?
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
--
Posted via http://www.ruby-forum.com/.
dblack@wobblini.net
2006-Mar-14 17:59 UTC
[Rails] What does the send() method do on a model object?
Hi -- On Tue, 14 Mar 2006, Wes Gamble 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.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 (dblack@wobblini.net) 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
2006-Mar-14 18:00 UTC
[Rails] Re: What does the send() method do on a model object?
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 Gamble 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-- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Mar-14 18:02 UTC
[Rails] Re: What does the send() method do on a model object?
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 Gamble 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-- Posted via http://www.ruby-forum.com/.
Dylan Stamat
2006-Mar-14 18:06 UTC
[Rails] Re: What does the send() method do on a model object?
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 :)
On 3/14/06, Ben Sinclair <ben@bensinclair.com>
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 Gamble 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
>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060314/11020247/attachment-0001.html
Maybe Matching Threads
- Wrap error_messages_for() call when no instance var present
- belongs_to <parent name>, :foreign_key modifier not working
- Using <%= text_field %> within partials is problematic
- checkboxes with a has_many :through relation
- has_many :through and foreign key parameters