Ruby on Rails - 哈希数组 - 基于键显示值

时间:2017-02-20 02:16:41

标签: ruby-on-rails arrays ruby postgresql hash

我有一个哈希数组保存到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错误...

<%= @item.yearly_interest_totals.detect do |t|
 t[:financial_year] == fin_year end [:total] %>

有人可以解释为什么我收到这个错误吗?

更新

哈希键和局部变量的名称相同都令人困惑,我已将局部变量更改为fin_year

&lt;%= fin_year.class%&gt;正在制作String

&lt;%= @ item.yearly_interest_totals.class%&gt;正在制作Array

&lt;%= @ item.yearly_interest_totals [0] [:financial_year] .class%&gt;正在返回“没有将符号隐式转换为整数”错误...

2 个答案:

答案 0 :(得分:2)

问题似乎是哈希数组中键:financial_year的值是字符串(例如&#34; 2017&#34;),但变量financial_year的值是一个fixnum /整数(例如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,您也可以&# 39; t似乎使用哈希键访问这些值(例如[:financial_year],[&#34; financial_year&#34;]等)。

经过一番挖掘,我发现了这个: Rails access hash value 接受的答案让我尝试了JSON.parse,我能够开始工作,尽管是.each而不是.detect。这次我在Rails 5应用程序中创建了一个Item模型,使用了Postgres,并播种了一个Item。我还没做的是创建一个控制器或任何视图。我通过Rails控制台执行了我的代码。因此,如果您复制我的代码并且它不适合您,则问题可能出在控制器和视图中。

最终,关于这种散列/ JSON区别以及实现如何使其显示为一个或另一个,仍有一些发现要做。

应用/模型/ item.rb的

class Item < ApplicationRecord
  validates :name, presence: true
end

<强>分贝/迁移/ 20170220221004_enable_hstore_extension.rb

class EnableHstoreExtension < ActiveRecord::Migration
  def change
    enable_extension 'hstore'
  end
end

<强>分贝/迁移/ 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

<强>分贝/ 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

答案 1 :(得分:0)

我不知道Rails 5,但也许这会有所帮助,Rails 4,假设financial_year是一个变量,我正确地理解了这个问题:

<% @item.yearly_interest_totals.each do |t| %>
<%= t['total'] == financial_year %>
<% end %>