Ruby-on-rails 如何在 Rails 中发现模型属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1289557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you discover model attributes in Rails?
提问by gbc
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.
我发现很难轻松查看所有模型类中存在哪些属性/属性,因为它们没有在我的类文件中明确定义。
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.
为了发现模型属性,我将 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?
我的问题是,当您第一次分析 Rails 代码库时,您如何发现模型属性?您是否始终保持 schema.rb 文件打开,或者是否有更好的方法不涉及不断在架构文件和模型文件之间跳转?
回答by penger
For Schema related stuff
对于 Schema 相关的东西
Model.column_names
Model.columns_hash
Model.columns
For instance variables/attributes in an AR object
例如 AR 对象中的变量/属性
object.attribute_names
object.attribute_present?
object.attributes
For instance methods without inheritance from super class
例如没有从超类继承的实例方法
Model.instance_methods(false)
回答by ez.
There is a rails plugin called Annotate models, that will generate your model attributes on the top of your model files here is the link:
有一个名为 Annotate models 的 rails 插件,它会在你的模型文件的顶部生成你的模型属性,这里是链接:
https://github.com/ctran/annotate_models
https://github.com/ctran/annotate_models
to keep the annotation in sync, you can write a task to re-generate annotate models after each deploy.
为了保持注释同步,您可以编写一个任务来在每次部署后重新生成注释模型。
回答by Nick
If you're just interested in the properties and data types from the database, you can use Model.inspect.
如果您只对数据库中的属性和数据类型感兴趣,则可以使用Model.inspect.
irb(main):001:0> User.inspect
=> "User(id: integer, email: string, encrypted_password: string,
reset_password_token: string, reset_password_sent_at: datetime,
remember_created_at: datetime, sign_in_count: integer,
current_sign_in_at: datetime, last_sign_in_at: datetime,
current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime,
updated_at: datetime)"
Alternatively, having run rake db:createand rake db:migratefor your development environment, the file db/schema.rbwill contain the authoritative source for your database structure:
或者,在运行rake db:create并rake db:migrate针对您的开发环境后,该文件db/schema.rb将包含您的数据库结构的权威来源:
ActiveRecord::Schema.define(version: 20130712162401) do
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
end
回答by Haris Krajina
To describe model I use following snippet
为了描述模型,我使用以下代码段
Model.columns.collect { |c| "#{c.name} (#{c.type})" }
Again this is if you are looking pretty print to describe you ActiveRecordwithout you going trough migrations or hopping that developer before you was nice enough to comment in attributes.
同样,这是如果您正在寻找漂亮的印刷品来描述您,ActiveRecord而无需在您足够好地评论属性之前进行迁移或跳过该开发人员。

