@post = Post.create(:name => 'test', :post_number => 20, :active => true)
保存后,我希望能够将该对象返回到哈希表中,例如通过执行以下操作:
@object.to_hash
在Rails中如何实现这个?
@post = Post.create(:name => 'test', :post_number => 20, :active => true)
保存后,我希望能够将该对象返回到哈希表中,例如通过执行以下操作:
@object.to_hash
在Rails中如何实现这个?
如果你只想获取属性,那么可以通过以下方式获得:
@post.attributes
请注意,每次调用此方法都会调用 ActiveModel::AttributeSet.to_hash
,因此如果您需要多次访问哈希表,则应将其缓存到本地变量中:
attribs = @post.attributes
在Rails的最新版本中(虽然无法确定具体是哪个版本),您可以使用as_json
方法:
@post = Post.first
hash = @post.as_json
puts hash.pretty_inspect
将输出:
{
:name => "test",
:post_number => 20,
:active => true
}
class Post < ActiveRecord::Base
def as_json(*args)
{
:name => "My name is '#{self.name}'",
:post_number => "Post ##{self.post_number}",
}
end
end
{
:name => "My name is 'test'",
:post_number => "Post #20"
}
@object.as_json
as_json提供了一种非常灵活的配置方式,可以根据模型关系配置复杂对象。
示例
模型campaign属于shop,并且有一个list
模型list有许多list_tasks,每个list_tasks都有许多comments
我们可以轻松地获取将所有这些数据组合在一起的json。
@campaign.as_json(
{
except: [:created_at, :updated_at],
include: {
shop: {
except: [:created_at, :updated_at, :customer_id],
include: {customer: {except: [:created_at, :updated_at]}}},
list: {
except: [:created_at, :updated_at, :observation_id],
include: {
list_tasks: {
except: [:created_at, :updated_at],
include: {comments: {except: [:created_at, :updated_at]}}
}
}
},
},
methods: :tags
})
注意方法::tags可以帮助您附加任何与其他对象没有关系的附加对象。您只需要在模型campaign中定义一个名为tags的方法即可。该方法应返回任何您需要的内容(例如Tags.all)。
as_json的官方文档。
to_
变体似乎与 as_
变体几乎完全相同,除了引用输出。唯一不喜欢的是没有保留我的筛选条件顺序。它似乎是按字母顺序排序的。我以为这与我环境中的 awesome_print gem 有关,但我认为情况并非如此。 - Pysis你可以使用以下方法将模型对象的属性作为哈希返回:
@post.attributes
或者。@post.as_json
as_json
允许您包括关联对象和它们的属性,并指定要包含/排除哪些属性(请参见文档)。然而,如果您只需要基本对象的属性,在我的应用程序中使用ruby 2.2.3和rails 4.2.2进行基准测试表明attributes
所需时间少于as_json
的一半。
>> p = Problem.last
Problem Load (0.5ms) SELECT "problems".* FROM "problems" ORDER BY "problems"."id" DESC LIMIT 1
=> #<Problem id: 137, enabled: true, created_at: "2016-02-19 11:20:28", updated_at: "2016-02-26 07:47:34">
>>
>> p.attributes
=> {"id"=>137, "enabled"=>true, "created_at"=>Fri, 19 Feb 2016 11:20:28 UTC +00:00, "updated_at"=>Fri, 26 Feb 2016 07:47:34 UTC +00:00}
>>
>> p.as_json
=> {"id"=>137, "enabled"=>true, "created_at"=>Fri, 19 Feb 2016 11:20:28 UTC +00:00, "updated_at"=>Fri, 26 Feb 2016 07:47:34 UTC +00:00}
>>
>> n = 1000000
>> Benchmark.bmbm do |x|
?> x.report("attributes") { n.times { p.attributes } }
?> x.report("as_json") { n.times { p.as_json } }
>> end
Rehearsal ----------------------------------------------
attributes 6.910000 0.020000 6.930000 ( 7.078699)
as_json 14.810000 0.160000 14.970000 ( 15.253316)
------------------------------------ total: 21.900000sec
user system total real
attributes 6.820000 0.010000 6.830000 ( 7.004783)
as_json 14.990000 0.050000 15.040000 ( 15.352894)
def to_hash
{ name: self.name, active: true }
end
这里有一些很好的建议。
我认为值得注意的是,您可以将ActiveRecord模型视为哈希表进行处理:
@customer = Customer.new( name: "John Jacob" )
@customer.name # => "John Jacob"
@customer[:name] # => "John Jacob"
@customer['name'] # => "John Jacob"
不确定这是否是你需要的,但在 Ruby 控制台中尝试以下内容:
h = Hash.new
h["name"] = "test"
h["post_number"] = 20
h["active"] = true
h
显然,它会在控制台返回一个哈希值。如果你想从方法中返回哈希值,而不仅仅是"h",可以尝试使用"return h.inspect",类似于下面的示例:
def wordcount(str)
h = Hash.new()
str.split.each do |key|
if h[key] == nil
h[key] = 1
else
h[key] = h[key] + 1
end
end
return h.inspect
end
我的解决方案:
Hash[ post.attributes.map{ |a| [a, post[a]] } ]
Swanand的答案很棒。
如果你正在使用FactoryGirl,你可以使用它的build
方法生成不带键id
的属性哈希表。例如:
build(:post).attributes
虽然这是一个老问题,但是被广泛引用...我认为大多数人使用其他方法,但实际上有一个to_hash
方法,它必须正确设置。通常,在Rails 4之后,pluck是更好的答案...我主要回答这个问题是因为我不得不搜索很多才能找到这个线程或任何有用的东西,并且假设其他人也遇到了同样的问题...
注意:不建议每个人都使用此方法,但适用于边缘情况!
从 Ruby on Rails API ... http://api.rubyonrails.org/classes/ActiveRecord/Result.html ...
This class encapsulates a result returned from calling #exec_query on any database connection adapter. For example:
result = ActiveRecord::Base.connection.exec_query('SELECT id, title, body FROM posts')
result # => #<ActiveRecord::Result:0xdeadbeef>
...
# Get an array of hashes representing the result (column => value):
result.to_hash
# => [{"id" => 1, "title" => "title_1", "body" => "body_1"},
{"id" => 2, "title" => "title_2", "body" => "body_2"},
...
] ...
.to_json
将会查询数据库。 - ykhrustalevjoins
和select
,Person.joins(:address).select("addresses.street, persons.name").find_by_id(id).attributes
可以返回以下结果:{ street: "", name: "" }
- Fangxingas_json
比attributes
更省钱。如果你查看as_json
的源代码,它会调用serializable_hash
,而serializable_hash
又会调用attributes
! 所以你的建议实际上增加了两个复杂度层次,使其更加昂贵。 - sandre89.to_json
将所有内容转换为字符串,而.attributes
保留类型信息。 - Steve Folly