# RSpec get model attribute values by name

**URL:** <https://www.ruby-forum.com/t/rspec-get-model-attribute-values-by-name/159055>\
**Category:** RSpec\
**Created:** [January 30, 2009, 9:45pm UTC](https://www.ruby-forum.com/t/rspec-get-model-attribute-values-by-name/159055 "2009-01-30T21:45:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 30, 2009, 9:45pm UTC](https://www.ruby-forum.com/t/rspec-get-model-attribute-values-by-name/159055/1 "2009-01-30T21:45:13Z")

</div>

This is a request for a programming technique:

Given

class Mymodel \< ActiveRecord::Base  
end

my\_instance = Mymodel.new

If I am given a string representation of an attribute “xxx” what is the  
most elegant way of passing that string to my\_instance to retrieve its  
value? This is probably a common idiom, given what Rails does, but I  
cannot seem to find its code in the rails gem at the moment.

So, if anyone knows hoe to do this off the cuff I would appreciate it.  
An example of the intended use of this is

def table\_compare(table1, table2, array\_of\_attributes) do |t1, t2,  
aon|  
tint = t1.column\_names & t2.column\_names  
tfin = tint & aon  
tfin.each do |column|  
return false if t1.column \<\> t2.column  
end  
return tfin #could be nil  
end

I want to convert the string value of the column variable into the  
attribute name to obtain the value of that attribute.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 30, 2009, 10:30pm UTC](https://www.ruby-forum.com/t/rspec-get-model-attribute-values-by-name/159055/2 "2009-01-30T22:30:05Z")

</div>

James B. wrote:

> This is a request for a programming technique:

Got it…

t1.read\_attribute(column)

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 30, 2009, 10:30pm UTC](https://www.ruby-forum.com/t/rspec-get-model-attribute-values-by-name/159055/3 "2009-01-30T22:30:27Z")

</div>

On Fri, Jan 30, 2009 at 3:45 PM, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> most elegant way of passing that string to my\_instance to retrieve its  
> tfin.each do |column|  
> return false if t1.column \<\> t2.column  
> end  
> return tfin #could be nil  
> end
> 
> I want to convert the string value of the column variable into the  
> attribute name to obtain the value of that attribute.

I’m assuming you want to treat your model instance like a hash:

m = MyModel.new :name =\> “foo”  
m[“name”] # =\> “foo”

[http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M001972&name=[]](http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M001972&name=%5B%5D)

On a different note, how about some better variable names?

–  
Zach D.  
[http://www.continuousthinking.com](http://www.continuousthinking.com)

> **[Homepage | Mutually Human](https://www.mutuallyhuman.com)**
>
> We empower businesses to overcome challenges with a holistic focus on People, Process, and Technology for lasting results and innovation.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 31, 2009, 4:01am UTC](https://www.ruby-forum.com/t/rspec-get-model-attribute-values-by-name/159055/4 "2009-01-31T04:01:59Z")

</div>

On Fri, Jan 30, 2009 at 3:56 PM, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## That code was for demonstration purposes for this question only.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> rspec-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)

a = “name”

As Zach pointed out, you can access AR instance attributes like a hash:  
my\_instance[a] = “foobar”  
my\_instance[a.to\_sym] # =\> “foobar”

the reason you can use either a symbol or a string is because the hash  
access is done with a HashWithIndifferentAccess.

A couple other things you can do…

my\_instance.send(a) # =\> “foobar”

It’s important to view method calls as sending messages to objects in  
Ruby… send is a method you can use to send a message to an object,  
where the method name is known only at runtime.

eval “my\_instance.#{a}”

and then of course you can eval strings…so if you have a var  
containing the name, you can interpolate it into another string and  
eval that.

I realize you didn’t ask for all of that but I’m feeling a bit chatty.

Pat

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 31, 2009, 12:56am UTC](https://www.ruby-forum.com/t/rspec-get-model-attribute-values-by-name/159055/5 "2009-01-31T00:56:30Z")

</div>

Zach D. wrote:

> m = MyModel.new :name =\> “foo”  
> m[“name”] # =\> “foo”
> 
> [http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M001972&name=[]](http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M001972&name=%5B%5D)
> 
> On a different note, how about some better variable names?

That code was for demonstration purposes for this question only.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 31, 2009, 3:18pm UTC](https://www.ruby-forum.com/t/rspec-get-model-attribute-values-by-name/159055/6 "2009-01-31T15:18:09Z")

</div>

Pat M. wrote:

> I realize you didn’t ask for all of that but I’m feeling a bit chatty.
> 
> Pat

The more information the better. I never really did understand exactly  
what using eval() was supposed to accomplish, now I do. Chat away…

I do want to point out that this is what the ActiveRecord::Base api has  
to say about the method:

Returns the value of the attribute identified by attr\_name after it has  
been typecast (for example, “2004-12-12” in a data column is cast to a  
date object, like Date.new(2004, 12, 12)). (Alias for the protected  
read\_attribute method).

Notice anything wrong about the api call description? Should not this  
say:

[attr\_name]

Returns the value of the attribute identified by attr\_name…

The reason that I raised this question to begin with was because I could  
not get “model\_instance” to work. Which is what the api is  
telling me to do the way that it is written. I came up with the  
alternative, write\_attribute(attr\_name), by looking at the code for .

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 31, 2009, 6:25pm UTC](https://www.ruby-forum.com/t/rspec-get-model-attribute-values-by-name/159055/7 "2009-01-31T18:25:16Z")

</div>

On Sat, Jan 31, 2009 at 6:18 AM, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> to say about the method:
> 
> [attr\_name]

Not quite…

You must be looking at the rdoc. RDoc looks at the method definition,  
and then includes any comments above it. Method definitions in ruby  
are of the form  
method\_name(param1, param2, \*other\_params, &block)

and is just a method call. You can define it on your own objects  
with  
def … end

Note that you can do  
{:foo =\> “abc”, :bar =\> “123”}.send :, :foo

I just looked at the RDoc, and yeah you’re right that it shows it of  
the form . That’s because RDoc just sticks in the method  
as it’s defined. You would need to know that this is a special case  
in Ruby where you really call it like blah[:foo]

Pat
