如何通过 ActiveRecord 行结果中的属性名称访问字段? [英] How to access fields by attribute name from ActiveRecord row results?

查看:43
本文介绍了如何通过 ActiveRecord 行结果中的属性名称访问字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然我在这里遗漏了一些明显的东西,但希望能举个简单的例子.

Apparently I'm missing something obvious here, but would appreciate a quick example.

我有一条从 ActiveRecord 返回的记录,我从查询中选择了几列.字段为 BigDecimal 格式,需要展平为字符串.

I've got a record coming back from ActiveRecord where I have a few columns selected from a query. The fields are of BigDecimal format and need to be flattened to string.

我最初认为通过以下方式提取查询就足够了:

I had initially thought it was sufficient to pull the query with:

rows = ModelName.order("date DESC").select('table_name.precise_number1, table_name.precise_number2').limit(100).all.zip
rows_stringified1 = Array.new
rows_stringified2 = Array.new

readings.each do |row|
  rows_stringified1.push row[:precise_number1].to_s
  rows_stringified2.push row[:precise_number2].to_s
end

然而,这会产生一个错误,例如can't convert Symbol into Integer.显然,我并没有完全遵循如何从行集结果中的记录访问列.

However, this yields an error such as can't convert Symbol into Integer. Obviously I'm not following exactly how to access columns from records in a row set result.

你通常会怎么做?

推荐答案

大概你有一个错字,你正在这样做:

Presumably you have a typo and you're doing this:

readings = ModelName.order("date DESC").
                     select('table_name.precise_number1, table_name.precise_number2').
                     limit(100).
                     all.
                     zip

而不是分配给 rows.注意到 zip 在结尾?那没有任何意义.当你这样做时:

instead of assigning to rows. Noticed that zip at the end? That doesn't make any sense. When you do this:

[a, b, c].zip

你明白了:

[[a], [b], [c]]

因此,在您的 readings.each 块中,row 实际上是 [model] 而不是 model 你认为它是,这意味着 row[:precise_number1] 正在尝试使用符号 访问 Array row:precise_number1 而不是数组期望的整数,因此您的无法将符号转换为整数"错误.

So, in your readings.each block, the row is in fact [model] rather than the model that you think it is and that means that row[:precise_number1] is trying to access the Array row using the Symbol :precise_number1 instead of the Integer that the Array expects, hence your "can't convert Symbol into Integer" error.

所以要么去掉 zip 并保留你的 each 原样:

So either get rid of the zip and leave your each as-is:

readings = ModelName.order("date DESC").
                     select('table_name.precise_number1, table_name.precise_number2').
                     limit(100).
                     all
# ...
readings.each do |row|
  rows_stringified1.push row[:precise_number1].to_s
  rows_stringified2.push row[:precise_number2].to_s
end

或保留 zip 并调整您的 each 块以匹配 row 真正的样子:

or keep the zip and adjust your each block to match what row really looks like:

readings = ModelName.order("date DESC").
                     select('table_name.precise_number1, table_name.precise_number2').
                     limit(100).
                     all.
                     zip
# ...
readings.each do |row|
  rows_stringified1.push row.first[:precise_number1].to_s
  rows_stringified2.push row.first[:precise_number2].to_s
end

我建议去掉 zip,因为它没有任何用处,只会让事情变得混乱.

I'd recommend getting rid of the zip as it does nothing useful and just confuses things.

这篇关于如何通过 ActiveRecord 行结果中的属性名称访问字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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