获取 ActiveRecord 对象中的属性类型

发布于 2024-08-24 09:48:12 字数 600 浏览 7 评论 0原文

我想知道是否可以以编程方式获取类型(如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

若水般的淡然安静女子 2024-08-31 09:48:12

在 Rails 3 中,对于模型“Driver”,您需要 Driver.columns_hash

Driver.columns_hash["name"].type  #returns :string

如果你想迭代它们,你可以这样做:

Driver.columns_hash.each {|k,v| puts "#{k} => #{v.type}"}

它将输出以下内容:

id => integer
name => string
created_at => datetime
updated_at => datetime

In Rails 3, for your model "Driver", you want Driver.columns_hash.

Driver.columns_hash["name"].type  #returns :string

If you want to iterate through them, you'd do something like this:

Driver.columns_hash.each {|k,v| puts "#{k} => #{v.type}"}

which will output the following:

id => integer
name => string
created_at => datetime
updated_at => datetime
梦里人 2024-08-31 09:48:12

在 Rails 5 中,您可以独立于数据库来执行此操作。如果您使用新的 Attributes API 来定义(附加)属性,这一点很重要。

从模型类获取所有属性:

pry> User.attribute_names
=> ["id",
 "firstname",
 "lastname",
 "created_at",
 "updated_at",
 "email",...

获取类型:

pry> User.type_for_attribute('email')
=> #<ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::MysqlString:0x007ffbab107698
 @limit=255,
 @precision=nil,
 @scale=nil>

有时这比需要的信息更多。有一个方便的函数可以将所有这些类型映射到一个核心集(:整数、:字符串等)。

> User.type_for_attribute('email').type
=> :string 

您还可以使用 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:

pry> User.attribute_names
=> ["id",
 "firstname",
 "lastname",
 "created_at",
 "updated_at",
 "email",...

Getting the type:

pry> User.type_for_attribute('email')
=> #<ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::MysqlString:0x007ffbab107698
 @limit=255,
 @precision=nil,
 @scale=nil>

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.)

> User.type_for_attribute('email').type
=> :string 

You can also get all that data in one call with attribute_types which returns a 'name': type hash.

山人契 2024-08-31 09:48:12

您可以通过执行以下操作来访问列的类型:

#script/console
Driver.columns.each {|c| puts c.type}

如果您想获取特定模型中所有列类型的列表,您可以执行以下操作:

Driver.columns.map(&:type) #gets them all
Driver.columns.map(&:type).uniq #gets the unique ones

You can access the types of the columns by doing this:

#script/console
Driver.columns.each {|c| puts c.type}

If you want to get a list of all column types in a particular Model, you could do:

Driver.columns.map(&:type) #gets them all
Driver.columns.map(&:type).uniq #gets the unique ones
偷得浮生 2024-08-31 09:48:12

在 Rails 5 中,这将为您提供所有字段名称及其数据类型的列表:

Model_Name.attribute_names.each do |k| puts "#{k} = #{Model_Name.type_for_attribute(k).type}" end

In rails 5 this will give you a list of all field names along with their data type:

Model_Name.attribute_names.each do |k| puts "#{k} = #{Model_Name.type_for_attribute(k).type}" end
情深已缘浅 2024-08-31 09:48:12

Rails 5+(也适用于虚拟属性):

Model.attribute_types['some_attribute'].type

Rails 5+ (works with virtual attributes as well):

Model.attribute_types['some_attribute'].type
情独悲 2024-08-31 09:48:12

此代码片段将为您提供模型的所有属性以及哈希中关联的数据库数据类型。只需将 Post 替换为您的 Active Record 模型即可。

Post.attribute_names.map {|n| [n.to_sym,Post.type_for_attribute(n).type]}.to_h

将返回这样的哈希值。

=> {:id=>:integer, :title=>:string, :body=>:text, :created_at=>:datetime, :updated_at=>:datetime, :topic_id=>:integer, :user_id=>:integer} 

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.

Post.attribute_names.map {|n| [n.to_sym,Post.type_for_attribute(n).type]}.to_h

Will return a hash like this.

=> {:id=>:integer, :title=>:string, :body=>:text, :created_at=>:datetime, :updated_at=>:datetime, :topic_id=>:integer, :user_id=>:integer} 
温柔嚣张 2024-08-31 09:48:12

假设 Foobar 是您的 Active Record 模型。您还可以这样做:

attributes = Foobar.attribute_names.each_with_object({}) do |attribute_name, hash|
  hash[attribute_name.to_sym] = Foobar.type_for_attribute(attribute_name).type
end

也可以在 Rails 4 上运行

Assuming Foobar is your Active Record model. You can also do:

attributes = Foobar.attribute_names.each_with_object({}) do |attribute_name, hash|
  hash[attribute_name.to_sym] = Foobar.type_for_attribute(attribute_name).type
end

Works on Rails 4 too

谈情不如逗狗 2024-08-31 09:48:12

在 Rails 4 中,您可以使用 Model.column_types

In Rails 4 You would use Model.column_types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文