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):
On 21 May 2015 at 13:48, kranthi kumar [email protected] wrote:
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
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
On 22 May 2015 at 07:36, kranthi kumar [email protected] 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