Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am learning some Ruby on Rails, and am a newbie. Most of my background is in ASP.net MVC on the back end.

As I play with a basic scaffold project, I wonder about this case: you jump into an established Rails project and want to get to know the model. Based on what I have seen so far (again, simple scaffold), the properties for a given class are not immediately revealed. I don't see property accessors on the model classes.

I do understand that this is because of the dynamic nature of Ruby and such things are not necessary or even perhaps desirable. Convention over code, I get that. (Am familiar with dynamic concepts, mostly via JS.)

But if I am somewhere off in a view, and want to quickly know whether the (eg) Person object has a MiddleName property, how would I find that out? I don't have to go into the migrations, do I?

share|improve this question
Have you tried using the rails console? –  Jeremy Heiler Jan 5 '11 at 3:57
add comment

1 Answer

ActiveRecord provides the attributes method, which returns a hash of attribute names mapped to their values for the receiving object. So you can do something like (example console session):

> u = User.first
> u.attributes
=> {"first_name"=>"Joe", "last_name"=>"Bloggs","email"=>"joe@bloggs.com"}
> u.attributes.keys
=> ["first_name", "last_name", "email"]
> u.attributes.keys.include?("middle_name")
=> false

Documentation link

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.