如何在Rails中发现模型属性? [英] How do you discover model attributes in Rails?
问题描述
我发现很难轻松查看所有模型类中存在哪些属性/属性,因为它们未在我的类文件中明确定义。
I am finding it difficult to easily see what attributes/properties exist on all of my model classes since they are not explicitly defined in my class files.
要发现模型属性,我保持schema.rb文件打开,并在它和需要编写的任何代码之间进行切换。这行得通,但是很麻烦,因为我必须在读取模式文件以选择属性,在模型类文件以检查方法以及我编写的用于调用属性&的任何新代码之间切换。方法。
To discover model attributes, I keep the schema.rb file open and flip between it and whatever code I'm writing as needed. This works but is clunky because I have to switch between reading the schema file to pick up attributes, the model class file to check methods, and whatever new code that I'm writing to call attributes & methods.
我的问题是,当您第一次分析Rails代码库时,如何发现模型属性?您是否始终将schema.rb文件保持打开状态,还是有一种更好的方法不涉及在架构文件和&模型文件不断?
My question is, how do you discover model attributes when you're analyzing a Rails codebase for the first time? Do you keep the schema.rb file open all the time, or is there a better way that doesn't involve jumping between schema file & model file constantly?
推荐答案
用于与架构相关的内容
Model.column_names
Model.columns_hash
Model.columns
AR对象中的实例变量/属性
For instance variables/attributes in an AR object
object.attribute_names
object.attribute_present?
object.attributes
例如没有从超类继承的实例方法
For instance methods without inheritance from super class
Model.instance_methods(false)
这篇关于如何在Rails中发现模型属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!