callum
callum

Reputation: 37829

How can you discover accessible attribute names from a model instance?

This answer says you can do object.attribute_names to get a list of attribute names for a model instance.

But is there any way to get a list of all its accessible attribute names?

Upvotes: 1

Views: 1588

Answers (1)

Daniel Rikowski
Daniel Rikowski

Reputation: 72524

You can use accessible_attributes.

You have to provide a role, because different roles can have different accessible attributes.

If you want to have the attributes from a model instance you can use this code:

@my_model.class.accessible_attributes(:admin) # Returns array of symbols

Upvotes: 3

Related Questions