Adding record with has_many relationship .with dynamic column_name

24 views
Skip to first unread message

kranthi kumar

unread,
May 21, 2015, 9:05:25 AM5/21/15
to rubyonra...@googlegroups.com
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 

NoMethodError (undefined method `<<' for nil:NilClass):



Colin Law

unread,
May 21, 2015, 10:46:49 AM5/21/15
to rubyonra...@googlegroups.com
On 21 May 2015 at 13:48, kranthi kumar <kranth...@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

kranthi kumar

unread,
May 22, 2015, 2:36:45 AM5/22/15
to rubyonra...@googlegroups.com
I am new to ruby on rails . I am trying to upload the data to any model , through file. so they specify the model name , and the data which needs to be dumped in to database . i am parsing each line of the file and trying to add the records . I am facing problem adding the has_many relations columns,

I am getting the column_names through model name. and data from the file. I need to add a record to database.

I will be helpful if you can suggest any better ways of doing this.

Thanks 
kranthi

Colin Law

unread,
May 22, 2015, 3:53:35 AM5/22/15
to rubyonra...@googlegroups.com
On 22 May 2015 at 07:36, kranthi kumar <kranth...@gmail.com> wrote:
> I am new to ruby on rails . I am trying to upload the data to any model ,
> through file. so they specify the model name , and the data which needs to
> be dumped in to database . i am parsing each line of the file and trying to
> add the records . I am facing problem adding the has_many relations columns,
>
> I am getting the column_names through model name. and data from the file. I
> need to add a record to database.
>
> I will be helpful if you can suggest any better ways of doing this.

OK, that is the sort of situation where it is reasonable to use that approach.

Did it work with send?

Colin

>
> Thanks
> kranthi
>
> On Thursday, 21 May 2015 20:16:49 UTC+5:30, Colin Law wrote:
>>
>> On 21 May 2015 at 13:48, kranthi kumar <kranth...@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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/447e090e-6941-4b75-a295-5433613d17b6%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages