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 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 <javascript:>> > 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/447e090e-6941-4b75-a295-5433613d17b6%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.