【问题标题】:Active Record without Rails: How to pass an Arbitrary hash to Active Record没有 Rails 的 Active Record:如何将任意散列传递给 Active Record
【发布时间】:2013-01-25 19:52:57
【问题描述】:

我正在尝试将以下 Hash 传递给 Active Record 进行保存:

[{
  "id"=>"WSECOUT", 
  "realtime_start"=>"2013-02-10", 
  "realtime_end"=>"2013-02-10", 
  "title"=>"Reserve Bank Credit - Securities Held Outright", 
  "observation_start"=>"1989-03-22", 
  "observation_end"=>"2013-02-06", 
  "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-08 08:32:33-06", 
  "popularity"=>"42", 
  "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."
  }]

我的 ruby​​ 类如下所示:

require 'rubygems'
require 'active_record'  
require 'logger'

ActiveRecord::Base.establish_connection(  
:adapter => "mysql2",  
:host => "localhost",  
:username => "root",
:password => "*********",
:database => "fred"  
)  

class Series < ActiveRecord::Base

  attr_accessible :id, :realtime_start, :realtime_end, :title, :observation_start,
              :observation_end, :frequency, :frequency_short, :units, :units_short, 
              :seasonal_adjustment, :seasonal_adjustment_short, :last_updated, 
              :popularity, :notes

end

require_relative 'wsecout'
@series = Wsecout.new.getSeries "wsecout"
@series = @series['series']

test = Series.create(@series)

@series 变量包含哈希。当我运行此代码时,该对象被创建为 mysql 中的行,但是字段中没有数据。我知道我在这里错过了一步,但我无法弄清楚是哪一步。另外,我的 Hash 包含一个 id 会不会有问题,因为 Active Record 会创建它自己的 id?

【问题讨论】:

  • 如果你的第一个例子是正确的,那么@series 包含一个带有一个哈希的数组。试试@series['series'][0] 什么的。或者,您可以发布puts @series['series'].inspect 的输出。
  • 成功了,谢谢。关于如何将 Hash 中的 :id 映射到与 Action Record :id 不同的 :id 的任何想法?在这种情况下,哈希的“id”为“WSECOUT”。有没有办法在我使用 Series.create(@series) 时将该“id”映射到一个名为 :series_id 的新字段?

标签: ruby-on-rails ruby activerecord hash


【解决方案1】:

回答您的第二个问题“将“id”映射到名为 :series_id 的新字段:

@series['series'][0]['series_id'] = @series['series'][0]['id']
@series['series'][0].delete('id')

或者,如果您想根据某些条件更改多个键,则在 if 条件中使用它,如下所示:

@series['series'][0].keys.each do |k|
  if(k == 'id')
    @series['series'][0]['series_id'] = @series['series'][0][k]
    @series['series'][0].delete(k)
  end
end

这将遍历散列的每个键,如果该键与 id 匹配,则添加另一个具有相同值的键 series_id 并删除 id

【讨论】:

  • 我尝试用@series 替换'h'并收到以下错误:'`block in
    ': unexpected return (LocalJumpError)'
  • 哦,我的错!刚刚更新了答案,去掉了return语句
  • 首先感谢您的帮助!这非常有效,我正在更新我原来的问题以反映您的更改。
  • 嘿.. 我再次更新了我的答案。只是检查一下。我只是觉得在您的情况下循环遍历哈希键是没有意义的。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多