[英]Ruby on Rails - Array of Hashes - Display value based on key
我有一个哈希数组保存到Rails 5 Postgres DB(Ruby 2.3.1)中。 我可以像这样在show.html.erb
页面上显示此内容:
<%= @item.yearly_interest_totals %>
显示:
[
"{:financial_year=>\"2017\", :total=>\"120.08\"}",
"{:financial_year=>\"2018\", :total=>\"237.32\"}",
"{:financial_year=>\"2019\", :total=>\"163.75\"}",
"{:financial_year=>\"2020\", :total=>\"87.95\"}",
"{:financial_year=>\"2021\", :total=>\"15.38\"}"
]
同样在此页面上,我有一个变量<%= fin_year %>
,显示2017
。
我正在尝试使用以下代码在视图中显示与此fin_year
键对应的值,但它没有给我提供no implicit conversion of Symbol into Integer
错误的no implicit conversion of Symbol into Integer
……
<%= @item.yearly_interest_totals.detect do |t|
t[:financial_year] == fin_year end [:total] %>
有人可以解释一下为什么我收到此错误吗?
更新资料
哈希键和局部变量都被命名为相同的名称,这令人困惑,我将局部变量更改为fin_year
。
<%= fin_year.class%>正在生成String
<%= @ item.yearly_interest_totals.class%>正在生成Array
<%= @ item.yearly_interest_totals [0] [:financial_year] .class%>返回“符号未隐式转换为整数的错误” ...
问题似乎是散列数组中的键:financial_year
的值是字符串(例如“ 2017”),但是变量financial_year
值却是fixnum / integer(例如2017)。 尝试使它们一致以进行比较,例如:
<%= @item.yearly_interest_totals.detect do |t|
t[:financial_year] == financial_year.to_s end [:total] %>
这是Rails控制台的输出,将两者进行了比较:
Running via Spring preloader in process 15647
Loading development environment (Rails 4.2.7.1)
2.3.3 :001 > item_yearly_interest_totals = [{ financial_year: "2017", total: "120.08" }, { financial_year: "2018", total: "237.32" }, { financial_year: "2019", total: "163.75" }, { financial_year: "2020", total: "87.95" }, { financial_year: "2021", total: "15.38" }]
=> [{:financial_year=>"2017", :total=>"120.08"}, {:financial_year=>"2018", :total=>"237.32"}, {:financial_year=>"2019", :total=>"163.75"}, {:financial_year=>"2020", :total=>"87.95"}, {:financial_year=>"2021", :total=>"15.38"}]
2.3.3 :002 > financial_year = 2017
=> 2017
2.3.3 :003 > item_yearly_interest_totals.detect do |t|
2.3.3 :004 > t[:financial_year] == financial_year end [:total]
NoMethodError: undefined method `[]' for nil:NilClass
.
.
.
2.3.3 :005 > item_yearly_interest_totals.detect do |t|
2.3.3 :006 > t[:financial_year] == financial_year.to_s end [:total]
=> "120.08"
2.3.3 :007 >
更新(02-20-2017)
我不完全了解Rails的区别在哪里或正在发生,这是问题的根源,但是即使执行@item.yearly_interest_totals[0].class
并获得Hash
,您似乎也无法访问使用哈希键的值(例如[:financial_year],[“ financial_year”]等)。
经过一番挖掘,我发现了这一点: Rails访问哈希值和可接受的答案使我尝试JSON.parse
,尽管使用.each
而不是.detect
,但我仍然可以使用.detect
。 这次,我在Rails 5应用程序中创建了一个Item
模型,使用了Postgres,并为单个Item
种子。 我仍然没有做的是创建一个控制器或任何视图。 我通过Rails控制台执行了代码。 因此,如果您复制我的代码但对您不起作用,则问题可能出在控制器和视图内。
最终,关于此哈希/ JSON的区别以及实现方式如何使其表现为一个或另一个,仍有一些发现需要完成。
app / models / item.rb
class Item < ApplicationRecord
validates :name, presence: true
end
db / migrate / 20170220221004_enable_hstore_extension.rb
class EnableHstoreExtension < ActiveRecord::Migration
def change
enable_extension 'hstore'
end
end
db / migrate / 20170220221129_create_item.rb
class CreateItem < ActiveRecord::Migration[5.0]
def change
create_table :items do |t|
t.string :name, null: false, index: { unique: true }
t.hstore :yearly_interest_totals, array: true
t.timestamps null: false
end
end
end
db / seeds.rb
Item.create(name: 'Sample Item', yearly_interest_totals: [{ financial_year: "2017", total: "120.08" }, { financial_year: "2018", total: "237.32" }, { financial_year: "2019", total: "163.75" }, { financial_year: "2020", total: "87.95" }, { financial_year: "2021", total: "15.38" }])
这是在Rails控制台中执行的代码:
Running via Spring preloader in process 19764
Loading development environment (Rails 5.0.1)
2.4.0 :001 > @item = Item.first
Item Load (1.4ms) SELECT "items".* FROM "items" ORDER BY "items"."id" ASC LIMIT $1 [["LIMIT", 1]]
=> #<Item id: 1, name: "Sample Item", yearly_interest_totals: [{"total"=>"120.08", "financial_year"=>"2017"}, {"total"=>"237.32", "financial_year"=>"2018"}, {"total"=>"163.75", "financial_year"=>"2019"}, {"total"=>"87.95", "financial_year"=>"2020"}, {"total"=>"15.38", "financial_year"=>"2021"}], created_at: "2017-02-20 22:25:14", updated_at: "2017-02-20 22:25:14">
2.4.0 :002 > @item.class
=> Item(id: integer, name: string, yearly_interest_totals: hstore, created_at: datetime, updated_at: datetime)
2.4.0 :003 > @item.yearly_interest_totals.class
=> Array
2.4.0 :004 > @item.yearly_interest_totals[0].class
=> Hash
2.4.0 :005 > financial_year = 2017
=> 2017
2.4.0 :006 > financial_year.class
=> Integer
2.4.0 :007 > selected_year_interest_total = nil
=> nil
2.4.0 :008 > selected_year_interest_total.class
=> NilClass
2.4.0 :009 > @item.yearly_interest_totals.each do |t|
2.4.0 :010 > puts JSON.parse(t["financial_year"]).class
2.4.0 :011 > if JSON.parse(t["financial_year"]) == financial_year
2.4.0 :012?> selected_year_interest_total = JSON.parse(t["total"])
2.4.0 :013?> end
2.4.0 :014?> end
Integer
Integer
Integer
Integer
Integer
=> [{"total"=>"120.08", "financial_year"=>"2017"}, {"total"=>"237.32", "financial_year"=>"2018"}, {"total"=>"163.75", "financial_year"=>"2019"}, {"total"=>"87.95", "financial_year"=>"2020"}, {"total"=>"15.38", "financial_year"=>"2021"}]
2.4.0 :015 > selected_year_interest_total
=> 120.08
2.4.0 :016 > selected_year_interest_total.class
=> Float
我不了解Rails 5,但也许会有所帮助,Rails 4,假设financial_year是变量,并且我正确理解了这个问题:
<% @item.yearly_interest_totals.each do |t| %>
<%= t['total'] == financial_year %>
<% end %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.