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

查看:167
本文介绍了你如何发现在Rails的模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始我的第一个Rails应用程序去年秋天,只好放在货架上数个月付款时,工作吸了我所有的时间。现在我很感兴趣,又回到了这个项目,并通过code阅读,找出其中我离开了。

I started my first Rails application last fall and had to put in on the shelf for a few months when paying work sucked up all of my time. I'm now interested in getting back to the project and reading through the code to figure out where I left off.

在运行时的Rails动态创建模型的事实属性,节省了大量重复输入的,但我发现很难轻松地发现哪些属性/我所有的模型类的存在性,因为他们没有明确在我的类文件中定义。要发现模型的属性,我把schema.rb文件打开,它和任何code我在写一个使用模型的属性之间翻转。这工作,但就是笨重,因为我要读模式文件回暖属性,模型类文件回暖的方法,而且我写调用属性和放什么新的code;的方法。

The fact that Rails dynamically creates model attributes at runtime saves a lot of repetitive typing, but I am finding it difficult to easily discover what attributes/properties exist on all of my model classes since they are not explicitly defined in my class files. To discover model attributes, I keep the schema.rb file open and flip between it and whatever code I'm writing that uses a model's properties. This works but is clunky because I have to read the schema file to pick up attributes, the model class file to pick up methods, and whatever new code that I'm writing to call attributes & methods.

所以我的问题是,你怎么发现模特属性当你分析一个Rails codeBase的首次?你会为了schema.rb文件打开所有的时间,或者是有更好的办法,不涉及架构文件与放大器之间的跳跃;模型文件不断?

So my question is, how do you discover model properties when you are 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屋!

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