On 21 May 2015 at 13:48, kranthi kumar <kranthi.3...@gmail.com> wrote:
> I know that we can add records like
>
> record.posts << Post.find(1);
>
> but how can add the same Post.find(1) record if I have a variable that
> contains the name  "posts"
>
> I have something like this
>
> column_name = "posts"
>
> record[column_name] << Post.find(1);
>
> it is throwing an error

The ruby method send can be used to call a method by name, so for
normal methods one would do
record.send(column_name) << Post.find(1)
I think that aught to work for an ActiveRecord relation but never tried it.
However when I see code like that it often meens that there is some
smelly code about.  Are you sure you really want to do what you are
attempting?  There may well be a better way.

Colin

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvrxc_eZqLw_K1u7t5c6RtmRSO2_Y8zU7nwj0%3Dx5cNpog%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to