Ruby on rails 通过Rails将JSON文件插入Postgres

Ruby on rails 通过Rails将JSON文件插入Postgres,ruby-on-rails,json,postgresql,Ruby On Rails,Json,Postgresql,我不熟悉如何在Rails中的Postgres DB中插入JSON。我看到您可以在rails中声明json列类型 现在我想运行命令Users.new(“Bob”,Bob),但得到错误: ArgumentError: wrong number of arguments (given 2, expected 0..1) bob = { name: "Bob", occupation: "Coder", pets: [ name: "Foo",

我不熟悉如何在Rails中的Postgres DB中插入JSON。我看到您可以在rails中声明json列类型

现在我想运行命令Users.new(“Bob”,Bob),但得到错误:

ArgumentError: wrong number of arguments (given 2, expected 0..1)

bob = {
      name: "Bob",
      occupation: "Coder",
      pets: [
        name: "Foo",
        type: "dog"
      ]
    }

class CreateUsers < ActiveRecord::Migration[5.1]
  def change
    create_table :users do |t|
      t.string :name
      t.json :data
    end
  end
end

我忘了添加要传递值的列的名称

Users.new("Bob", bob) 
变成

Users.new(name: "Bob", data: bob)

您需要指定列的名称。试试这个:

Users.new(name: "Bob", data: bob)

您是否考虑过在表中使用
jsonb
列?也许值得你花时间。我发现这篇文章非常有用:
Users.new(name: "Bob", data: bob)