# From string to field name

**URL:** <https://www.ruby-forum.com/t/from-string-to-field-name/188616>\
**Category:** Rails\
**Created:** [May 14, 2010, 8:26pm UTC](https://www.ruby-forum.com/t/from-string-to-field-name/188616 "2010-05-14T20:26:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Viorel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bcef8e/32.png) [@Viorel](https://www.ruby-forum.com/u/Viorel)\
**Post date:** [May 14, 2010, 8:26pm UTC](https://www.ruby-forum.com/t/from-string-to-field-name/188616/1 "2010-05-14T20:26:15Z")

</div>

I have something like this:  
for xx in ‘01’…‘09’  
field\_name=‘field’+xx  
puts ModelName.field\_name  
end

My problem is that ModelName has no field named field\_name, it have  
fields like field01, field02,…,field09. So, how do I transform a  
string in a field name?

It might be a trivial solution, but I can’t find it. I would  
appreciate any help.

---

<div class="post-metadata">

**Author:** ![Viorel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bcef8e/32.png) [@Viorel](https://www.ruby-forum.com/u/Viorel)\
**Post date:** [May 14, 2010, 8:35pm UTC](https://www.ruby-forum.com/t/from-string-to-field-name/188616/2 "2010-05-14T20:35:37Z")

</div>

I am trying to access those fields

---

<div class="post-metadata">

**Author:** ![Viorel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bcef8e/32.png) [@Viorel](https://www.ruby-forum.com/u/Viorel)\
**Post date:** [May 14, 2010, 8:28pm UTC](https://www.ruby-forum.com/t/from-string-to-field-name/188616/3 "2010-05-14T20:28:00Z")

</div>

What exactly are you trying to do with this?

On Fri, May 14, 2010 at 2:24 PM, Viorel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It might be a trivial solution, but I can’t find it. I would  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–

* * *

Joshua S. Martin

CONFIDENTIALITY NOTE: This e-mail message, including any attachment(s),  
contains information that may be confidential, protected by the attorney  
client or other legal privileges, and or proprietary non public  
information.  
If you are not an intended recipient of this message or an authorized  
assistant to an intended recipient, please notify the sender by replying  
to  
this message and then delete it from your system. Use, dissemination,  
distribution, or reproduction of this message and or any of its  
attachments  
(if any) by unintended recipients is not authorized and may be unlawful.

---

<div class="post-metadata">

**Author:** ![Viorel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bcef8e/32.png) [@Viorel](https://www.ruby-forum.com/u/Viorel)\
**Post date:** [May 14, 2010, 8:36pm UTC](https://www.ruby-forum.com/t/from-string-to-field-name/188616/4 "2010-05-14T20:36:47Z")

</div>

On May 14, 9:26 pm, Joshua M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What exactly are you trying to do with this?  
> I am trying to access these fields

---

<div class="post-metadata">

**Author:** ![Viorel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bcef8e/32.png) [@Viorel](https://www.ruby-forum.com/u/Viorel)\
**Post date:** [May 14, 2010, 8:42pm UTC](https://www.ruby-forum.com/t/from-string-to-field-name/188616/5 "2010-05-14T20:42:39Z")

</div>

Thank you Michel! I works!

---

<div class="post-metadata">

**Author:** ![Viorel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bcef8e/32.png) [@Viorel](https://www.ruby-forum.com/u/Viorel)\
**Post date:** [May 14, 2010, 8:37pm UTC](https://www.ruby-forum.com/t/from-string-to-field-name/188616/6 "2010-05-14T20:37:58Z")

</div>

On 14 May 2010 19:24, Viorel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My problem is that ModelName has no field named field\_name, it have  
> fields like field01, field02,…,field09. So, how do I transform a  
> string in a field name?

puts ModelName.send(field\_name)

---

<div class="post-metadata">

**Author:** ![Viorel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bcef8e/32.png) [@Viorel](https://www.ruby-forum.com/u/Viorel)\
**Post date:** [May 14, 2010, 9:11pm UTC](https://www.ruby-forum.com/t/from-string-to-field-name/188616/7 "2010-05-14T21:11:56Z")

</div>

Or model\_name.read\_attribute(field\_name)

However if you multiple fields like that, it sounds like you need a  
has\_many relationship.

has\_many :field\_names

object.field\_names.each do |field\_mame|

# do something

end

---

<div class="post-metadata">

**Author:** ![Viorel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bcef8e/32.png) [@Viorel](https://www.ruby-forum.com/u/Viorel)\
**Post date:** [May 14, 2010, 9:23pm UTC](https://www.ruby-forum.com/t/from-string-to-field-name/188616/8 "2010-05-14T21:23:38Z")

</div>

On May 14, 10:10 pm, David [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Great. Now normalize your database schema to get rid of those repeating  
> > fields.

You are absolutely right, both of you. It is not my schema, but I hope  
that your answers will persuade those responsible. Than you!

---

<div class="post-metadata">

**Author:** ![Viorel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/bcef8e/32.png) [@Viorel](https://www.ruby-forum.com/u/Viorel)\
**Post date:** [May 14, 2010, 8:51pm UTC](https://www.ruby-forum.com/t/from-string-to-field-name/188616/9 "2010-05-14T20:51:33Z")

</div>

[Please quote when replying]

Viorel wrote:

> Thank you Michel! I works!

Great. Now normalize your database schema to get rid of those repeating  
fields.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
