Rails无法在数据库中保存json

时间:2015-10-02 17:47:48

标签: ruby-on-rails ruby json postgresql

我想在我的Rails PostgreSQL数据库中保存Json数据。

在我的迁移文件中

class IbmSubscription < ActiveRecord::Migration
  def up
    create_table :ibm_subscriptions do |t|
      t.json 'ibm_response'
      t.references :user, index: true
      t.timestamps
    end
  end

  def down
    drop_table :ibm_subscriptions
  end    
end

我无法在ibm_response中保存JSON数据 我在rails console中试过这个

2.1.5 :004 > a = JSON.parse(@uri.to_s)
 => {"type"=>"SUBSCRIPTION_ORDER", "marketplace"=>{"base_url"=>"https://acme.appdirect.com", "partner"=>"ACME"}, "flag"=>"STATELESS", "creator"=>{"email"=>"test-email+creator@appdirect.com", "first_name"=>"DummyCreatorFirst", "language"=>"fr", "last_name"=>"DummyCreatorLast", "open_id"=>"https://www.appdirect.com/openid/id/ec5d8eda-5cec-444d-9e30-125b6e4b67e2", "uuid"=>"ec5d8eda-5cec-444d-9e30-125b6e4b67e2"}, "payload"=>{"company"=>{"country"=>"CA", "email"=>"company-email@example.com", "name"=>"Example Company Name", "phone_number"=>"415-555-1212", "uuid"=>"d15bb36e-5fb5-11e0-8c3c-00262d2cda03", "website"=>"http://www.example.com"}, "configuration"=>{"entry"=>{"key"=>"domain", "value"=>"mydomain"}}, "order"=>{"edition_code"=>"BASIC", "pricing_duration"=>"MONTHLY", "item"=>[{"quantity"=>"10", "unit"=>"USER"}, {"quantity"=>"15", "unit"=>"MEGABYTE"}]}}, "return_url"=>"https://www.appdirect.com/finishprocure?token=dummyOrder", "@xmlns:atom"=>"http://www.w3.org/2005/Atom"} 
2.1.5 :005 > a = IbmSubscription.create(ibm_response: @uri)
WARNING: Can't mass-assign protected attributes for IbmSubscription: ibm_response

   (0.2ms)  BEGIN
  SQL (5.7ms)  INSERT INTO "ibm_subscriptions" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"  [["created_at", Fri, 02 Oct 2015 17:30:13 UTC +00:00], ["updated_at", Fri, 02 Oct 2015 17:30:13 UTC +00:00]]
   (93.9ms)  COMMIT
 => #<IbmSubscription id: 1, ibm_response: nil, user_id: nil, created_at: "2015-10-02 17:30:13", updated_at: "2015-10-02 17:30:13"> 

创建ibm_response:nil。

请帮帮我。谢谢

1 个答案:

答案 0 :(得分:1)

  

无法为IbmSubscription

批量分配受保护的属性

此行表示您需要在:ibm_response模型中将IbmSubscription添加到a​​ttr_accessible。