How do you discover model attributes in Rails?

gbc picture gbc · Aug 17, 2009 · Viewed 102.1k times · Source

I am finding it difficult to easily see what attributes/properties exist on all of my model classes since they are not explicitly defined in my class files.

To discover model attributes, I keep the schema.rb file open and flip between it and whatever code I'm writing as needed. This works but is clunky because I have to switch between reading the schema file to pick up attributes, the model class file to check methods, and whatever new code that I'm writing to call attributes & methods.

My question is, how do you discover model attributes when you're analyzing a Rails codebase for the first time? Do you keep the schema.rb file open all the time, or is there a better way that doesn't involve jumping between schema file & model file constantly?

Answer

penger picture penger · Aug 18, 2009

For Schema related stuff

Model.column_names         
Model.columns_hash         
Model.columns 

For instance variables/attributes in an AR object

object.attribute_names                    
object.attribute_present?          
object.attributes

For instance methods without inheritance from super class

Model.instance_methods(false)