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

妖精的绣舞 提交于 2020-01-03 03:42:10

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/14568694/how-can-you-discover-accessible-attribute-names-from-a-model-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!