Hi - I am adding a series of records within a for loop using i as my counter. Rather than hard code the model attributes I would like to dynamically create them. Here''s what I''m trying to do: I have several fields on my example model named such as: spec_1_english spec_2_english etc... This is what I would like to do - create these attributes dynamically using my counter. for i in (0..2) example.spec_#{i}_english = "test value" l.save end Any help appreciated. Thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You could use #send. Try something like this: (0..2).each do |i| example.send("spec_#{i}_english=", "test value") l.save end Do you really want l.save instead of example.save, though? Maybe there''s some context missing from your code snippet. Regards, Craig --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Excellent - this worked great. Another tool for me to use. You''re correct l. was an error on my part in my snippet. Thanks Craig On Feb 18, 12:15 am, Craig Demyanovich <cdemyanov...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You could use #send. Try something like this: > > (0..2).each do |i| > example.send("spec_#{i}_english=", "test value") > l.save > end > > Do you really want l.save instead of example.save, though? Maybe there''s > some context missing from your code snippet. > > Regards, > Craig--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---