0

我有一个存储在一个名为的实例变量中的字段哈希:

 @series_from_fred

当我将此哈希传递给

 Serial.create(@series_from_fred) 

新的 Serial(模型名称)“行”被创建,散列中的参数映射到串行模型行中的字段。

但是,当我尝试通过

 serial.update_attributes(@series_from_fred)

请注意,“串行”是

 @serial.each do |serial|

我收到以下错误:

NoMethodError in AdminsController#checkSerials

undefined method `stringify_keys' for #<Array:0x007fd1fe3085e0>

有任何想法吗?

如果我进入 rails 控制台并输入“Serial”,就会发生这种情况

Serial(id: integer, series_name: string, realtime_start: string, 
realtime_end: string, title: string, observation_start: string, 
observation_end: string, frequency: string, frequency_short: string, 
units: string, units_short: string, seasonal_adjustment: string, 
seasonal_adjustment_short: string, last_updated: string, popularity: string, 
notes: text, created_at: datetime, updated_at: datetime)

由于我在运行时从 api 获取数据,如果我进入 rails 控制台并输入“@series_from_fred”,我将返回“nil”。但是,使用 logger.info(@series_from_fred.to_s) 我可以在终端输出中“看到”@series_from_fred 的值。该输出是:

[{"realtime_start"=>"2013-02-28", "realtime_end"=>"2013-02-28", 
"title"=>"Reserve Bank Credit - Securities Held Outright", 
"observation_start"=>"1989-03-22", "observation_end"=>"2013-02-20", 
"frequency"=>"Weekly, Ending Wednesday", "frequency_short"=>"W", 
"units"=>"Billions of Dollars", "units_short"=>"Bil. of $", 
"seasonal_adjustment"=>"Not Seasonally Adjusted", "seasonal_adjustment_short"=>"NSA", 
"last_updated"=>"2013-02-22 08:32:54-06", "popularity"=>"47", "notes"=>"The amount of 
securities held by Federal Reserve Banks. This quantity is the cumulative result of 
permanent open market operations: outright purchases or sales of securities, conducted 
by the Federal Reserve. Section 14 of the Federal Reserve Act defines the securities 
that the Federal Reserve is authorized to buy and sell.", "series_name"=>"WSECOUT"}] 
4

1 回答 1

0

虽然我不确定为什么

Serial.create(@series_from_fred) worked 

serial.update_attributes(@series_from_fred) 

行不通。我为“解决”该问题所做的是将我从 api 获得的 xml 转换为通过以下方式的哈希:

@series_from_fred = Hash.from_xml(series_responseHolder)

将此传递给:

serial.update_attributes(@series_from_fred)

成功更新了串行模型中该记录/对象/行的参数。

于 2013-02-28T22:58:47.340 回答