# Interpolation

**URL:** <https://www.ruby-forum.com/t/interpolation/160612>\
**Category:** Rails\
**Created:** [February 18, 2009, 5:57am UTC](https://www.ruby-forum.com/t/interpolation/160612 "2009-02-18T05:57:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![northband](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecae2f/32.png) [@northband](https://www.ruby-forum.com/u/northband)\
**Post date:** [February 18, 2009, 5:57am UTC](https://www.ruby-forum.com/t/interpolation/160612/1 "2009-02-18T05:57:05Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![northband](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecae2f/32.png) [@northband](https://www.ruby-forum.com/u/northband)\
**Post date:** [February 18, 2009, 6:15am UTC](https://www.ruby-forum.com/t/interpolation/160612/2 "2009-02-18T06:15:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![northband](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecae2f/32.png) [@northband](https://www.ruby-forum.com/u/northband)\
**Post date:** [February 18, 2009, 6:27am UTC](https://www.ruby-forum.com/t/interpolation/160612/3 "2009-02-18T06:27:32Z")

</div>

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
