# Objects and attributes

**URL:** <https://www.ruby-forum.com/t/objects-and-attributes/148143>\
**Category:** Rails\
**Created:** [September 9, 2008, 7:25pm UTC](https://www.ruby-forum.com/t/objects-and-attributes/148143 "2008-09-09T19:25:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [September 9, 2008, 7:25pm UTC](https://www.ruby-forum.com/t/objects-and-attributes/148143/1 "2008-09-09T19:25:20Z")

</div>

I have this method that it’s soul purpose is to toggle a field and then  
redisplay a chech\_box helper and a image signifying that the field has  
been updated. My issue, however, is that this method is to toggle  
several different field all via one method simply because the code to do  
so was all the same code save for the name of the field. I pass a value  
in which is the name of the field that I am manipulating, for example:

def method\_1(attribute\_name)  
…  
end

In that method I find a user based on an id and then I need to set an  
attribute based on the value passed in, so I have:

@user.find\_by\_id(params[:id])

@user."#{attribute\_name}"

However, that last bit of code doesn’t work. Is there a way to do what I  
am trying to do, that is, use dot notion on an object to update a field  
whose value is being passed into the method? Thanks in advance,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [September 9, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/objects-and-attributes/148143/2 "2008-09-09T20:28:16Z")

</div>

try to do

@user[attribute\_name] = value

if you pass a string or a symbol =]

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [September 9, 2008, 9:16pm UTC](https://www.ruby-forum.com/t/objects-and-attributes/148143/3 "2008-09-09T21:16:59Z")

</div>

On 9 Sep 2008, at 18:25, Shandy N. wrote:

> @user."#{attribute\_name}"
> 
> However, that last bit of code doesn’t work. Is there a way to do  
> what I  
> am trying to do, that is, use dot notion on an object to update a  
> field  
> whose value is being passed into the method? Thanks in advance,

You can use send to call methods by name.

Fred

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [September 10, 2008, 12:05am UTC](https://www.ruby-forum.com/t/objects-and-attributes/148143/4 "2008-09-10T00:05:37Z")

</div>

Fabio Eidi wrote:

> try to do
> 
> @user[attribute\_name] = value
> 
> if you pass a string or a symbol =]

That did the trick. Thx,

-S
