你如何在 Rails 中发现模型属性? [英] How do you discover model attributes in Rails?

查看:22
本文介绍了你如何在 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?

推荐答案

对于 Schema 相关的东西

For Schema related stuff

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆