# Accessing model attribute name dynamically

**URL:** <https://www.ruby-forum.com/t/accessing-model-attribute-name-dynamically/171892>\
**Category:** Rails\
**Created:** [July 10, 2009, 10:31pm UTC](https://www.ruby-forum.com/t/accessing-model-attribute-name-dynamically/171892 "2009-07-10T22:31:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bikle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@bikle](https://www.ruby-forum.com/u/bikle)\
**Post date:** [July 10, 2009, 10:31pm UTC](https://www.ruby-forum.com/t/accessing-model-attribute-name-dynamically/171892/1 "2009-07-10T22:31:43Z")

</div>

I’m calling this action from within another action:

edit\_meta(@table\_meta\_id, @field, @content)

where @field is an attribute name for a Model. @table\_meta\_id is the id  
and @content is the new value for that attribute.

The edit\_meta action looks like this:

def edit\_meta(id, field, content)

```
@meta = Model.find(:all, :conditions => ["id = ?", id])
@meta.field = content
@meta.updated_at = now()
@meta.save

```

end

Problem is that I can’t figure out how to get the field argument to tell  
my model which attribute I want to change. I just get a “NoMethodError”.  
How do I do that?

Thanks!

---

<div class="post-metadata">

**Author:** ![bikle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@bikle](https://www.ruby-forum.com/u/bikle)\
**Post date:** [July 10, 2009, 10:38pm UTC](https://www.ruby-forum.com/t/accessing-model-attribute-name-dynamically/171892/2 "2009-07-10T22:38:29Z")

</div>

How about:

@meta.send("#{field}=", content)

Jamey

On Fri, Jul 10, 2009 at 4:31 PM, Dan

---

<div class="post-metadata">

**Author:** ![bikle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@bikle](https://www.ruby-forum.com/u/bikle)\
**Post date:** [July 13, 2009, 6:32pm UTC](https://www.ruby-forum.com/t/accessing-model-attribute-name-dynamically/171892/3 "2009-07-13T18:32:44Z")

</div>

Jamey C. wrote:

> How about:
> 
> @meta.send("#{field}=", content)
> 
> Jamey
> 
> On Fri, Jul 10, 2009 at 4:31 PM, Dan

That’s it. Thanks!

---

<div class="post-metadata">

**Author:** ![bikle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@bikle](https://www.ruby-forum.com/u/bikle)\
**Post date:** [July 13, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/accessing-model-attribute-name-dynamically/171892/4 "2009-07-13T18:45:38Z")

</div>

Another alternative:

…  
@meta[field] = content  
…

Jeff

On Jul 13, 9:32 am, Dan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bikle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@bikle](https://www.ruby-forum.com/u/bikle)\
**Post date:** [July 13, 2009, 6:58pm UTC](https://www.ruby-forum.com/t/accessing-model-attribute-name-dynamically/171892/5 "2009-07-13T18:58:57Z")

</div>

Jeff B.systems wrote:

> Another alternative:
> 
> …  
> @meta[field] = content  
> …
> 
> Jeff
> 
> On Jul 13, 9:32ï¿½am, Dan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

I tried that one, and it didn’t work … error message said Ruby  
couldn’t translate {field} into an integer, even though I wanted a  
string… But thanks.

---

<div class="post-metadata">

**Author:** ![bikle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@bikle](https://www.ruby-forum.com/u/bikle)\
**Post date:** [July 13, 2009, 7:23pm UTC](https://www.ruby-forum.com/t/accessing-model-attribute-name-dynamically/171892/6 "2009-07-13T19:23:39Z")

</div>

Maybe I’m not understanding what you’re sending as a val for @field,  
because if it’s a string of the model attribute name, it should work:

$ script/console

> > klass = Klass.find(:first)  
> > =\> #\<Klass …\>

> > klass.foo  
> > =\> “bar”

> > klass[“foo”]  
> > =\> “bar”

> > klass[“foo”] = “biz”  
> > =\> “biz”

> > klass[“foo”]  
> > =\> “biz”

> > klass.foo  
> > =\> “biz”

Jeff

On Jul 13, 9:58 am, Dan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
