dB'
dB'

Reputation: 8330

Rails: how to list all accessible attributes of an ActiveRecord?

Say that I have a data model called Widget. If I pick one widget...

widget = Widget.find(1) 

... then I can get its attributes with widget.attributes.keys.

But what if I don't want to find a Widget?

Is there a way to list all the attributes of the Widget class (which inherits from ActiveRecord::Base) without having to retrieve one particular Widget first?

Upvotes: 2

Views: 3658

Answers (2)

dgilperez
dgilperez

Reputation: 10796

If you need "accessible attributes", meaning attributes marked as accessible with attr_accessible, then you can use Widget.accessible_attributes.

Upvotes: 1

megas
megas

Reputation: 21791

You can get attributes directly from data model:

Widget.column_names

Upvotes: 6

Related Questions