views:

500

answers:

3

I started my first Rails application last fall and had to put in on the shelf for a few months when paying work sucked up all of my time. I'm now interested in getting back to the project and reading through the code to figure out where I left off.

The fact that Rails dynamically creates model attributes at runtime saves a lot of repetitive typing, but I am finding it difficult to easily discover 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 that uses a model's properties. This works but is clunky because I have to read the schema file to pick up attributes, the model class file to pick up methods, and whatever new code that I'm writing to call attributes & methods.

So my question is, how do you discover model properties when you are 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?

+2  A: 
some_class.attributes

Source: blog

mcandre
some_class.attributes.keys is a little cleaner
klochner
wonder if any IDEs use this for autocompletion? Seems like an obvious thing to do for a rails model. I'm always disappointed when I start typing an attribute name and it doesn't autocomplete.
frankodwyer
+4  A: 

There is a rails plugin called Annotate models, that will generate your model attributes on the top of your model files here is the link:

http://agilewebdevelopment.com/plugins/annotate%5Fmodels

to keep the annotation in sync, you can write a task to re-generate annotate models after each deploy.

ez
+4  A: 

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)

penger