views:

17

answers:

1

Let's say I have an Foo ActiveRecord model with fields foo_id, foo_name and foo_description.

After doing something like @foo = Foo.find(1), is there any method "model_fields" such that :

@foo.model_fields() would return the array ["foo_id", "foo_name", "foo_description"] ?

Thanks for the help,

Nicolas.

+3  A: 

There is an attributes method that give a hash of field and values. So you could use

@foo.attributes.keys

To get an array of the fields on the model.

There's also a Foo.column_names class method that gives you the same information without having to look up a record first.

Documentation for:
ActiveRecord::Base.column_names
ActiveRecord::Base#attributes

Emily
Thanks Emily for the quick answer! I am using .attributes= all the time. Not sure why I didn't think about that...
Nicolas M.