获取 ActiveRecord 对象中的属性类型
我想知道是否可以以编程方式获取类型(如 AR 所知 - 例如在迁移脚本和数据库中)(我知道数据存在于某处)。
例如,我可以处理所有属性名称:
ar.attribute_names.each { |name| puts name }
.attributes 仅返回名称到其当前值的映射(例如,如果未设置字段,则没有类型信息)。
我在某些地方看到过它带有类型信息:
在脚本/控制台中,键入 AR 实体的名称:
>> Driver
=> Driver(id: integer, name: string, created_at: datetime, updated_at: datetime)
很明显它知道类型。此外,还有 .column_for_attribute,它采用 attr 名称并返回一个列对象 - 该对象的类型埋藏在底层数据库列对象中,但它似乎不是获取它的干净方法。
我也感兴趣是否有一种方法对即将到来的新“ActiveModel”(rails3)友好并且与数据库细节分离(但也许类型信息不会成为其中的一部分,我似乎无法看看是否是)。
谢谢。
I would like to know if it is possible to get the types (as known by AR - eg in the migration script and database) programmatically (I know the data exists in there somewhere).
For example, I can deal with all the attribute names:
ar.attribute_names.each { |name| puts name }
.attributes just returns a mapping of the names to their current values (eg no type info if the field isn't set).
Some places I have seen it with the type information:
in script/console, type the name of an AR entity:
>> Driver
=> Driver(id: integer, name: string, created_at: datetime, updated_at: datetime)
So clearly it knows the types. Also, there is .column_for_attribute, which takes an attr name and returns a column object - which has the type buried in the underlying database column object, but it doesn't appear to be a clean way to get it.
I would also be interested in if there is a way that is friendly for the new "ActiveModel" that is coming (rails3) and is decoupled from database specifics (but perhaps type info will not be part of it, I can't seem to find out if it is).
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在 Rails 3 中,对于模型“Driver”,您需要
Driver.columns_hash
。如果你想迭代它们,你可以这样做:
它将输出以下内容:
In Rails 3, for your model "Driver", you want
Driver.columns_hash
.If you want to iterate through them, you'd do something like this:
which will output the following:
在 Rails 5 中,您可以独立于数据库来执行此操作。如果您使用新的 Attributes API 来定义(附加)属性,这一点很重要。
从模型类获取所有属性:
获取类型:
有时这比需要的信息更多。有一个方便的函数可以将所有这些类型映射到一个核心集(:整数、:字符串等)。
您还可以使用 attribute_types 在一次调用中获取所有数据,该调用返回
'name': type
哈希。In Rails 5, you can do this independently of the Database. That's important if you use the new Attributes API to define (additional) attributes.
Getting all attributes from a model class:
Getting the type:
That's sometimes more information than needed. There's a convenience function that maps all these types down to a core set (:integer, :string etc.)
You can also get all that data in one call with attribute_types which returns a
'name': type
hash.您可以通过执行以下操作来访问列的类型:
如果您想获取特定模型中所有列类型的列表,您可以执行以下操作:
You can access the types of the columns by doing this:
If you want to get a list of all column types in a particular Model, you could do:
在 Rails 5 中,这将为您提供所有字段名称及其数据类型的列表:
In rails 5 this will give you a list of all field names along with their data type:
Rails 5+(也适用于虚拟属性):
Rails 5+ (works with virtual attributes as well):
此代码片段将为您提供模型的所有属性以及哈希中关联的数据库数据类型。只需将 Post 替换为您的 Active Record 模型即可。
将返回这样的哈希值。
This snippet will give you all the attributes of a model with the associated database data types in a hash. Just replace Post with your Active Record Model.
Will return a hash like this.
假设 Foobar 是您的 Active Record 模型。您还可以这样做:
也可以在 Rails 4 上运行
Assuming
Foobar
is your Active Record model. You can also do:Works on Rails 4 too
在 Rails 4 中,您可以使用 Model.column_types。
In Rails 4 You would use Model.column_types.