如何从模型实例中发现可访问的属性名称? [英] How can you discover accessible attribute names from a model instance?
本文介绍了如何从模型实例中发现可访问的属性名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个答案 说您可以执行 object.attribute_names
来获取属性名称列表用于模型实例.
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?
推荐答案
您可以使用 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
这篇关于如何从模型实例中发现可访问的属性名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文