【问题标题】:Undefined method `json' for #<ActiveRecord:#<ActiveRecord 的未定义方法“json”:
【发布时间】:2017-12-17 11:36:15
【问题描述】:

我有一个如下所示的 create_table 迁移:

class CreateOrders < ActiveRecord::Migration[5.1]
  def change
    create_table :orders do |t|
      t.string :state
      t.string :coin_sku
      t.monetize :amount, currency: { present: false }
      t.json :payment

      t.timestamps
    end
  end
end

现在,当我运行 rails db:migrate 时,它​​会返回一条错误消息:

rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

undefined method `json' for #<ActiveRecord::ConnectionAdapters::SQLite3::TableDefinition:0x007f819fbb81b8>
Did you mean?  JSON

以前有人遇到过这个问题吗?非常感谢!!

【问题讨论】:

  • SQLite 是否支持 JSON 列?这可能是您需要在该数据库上使用 text 的东西。

标签: ruby-on-rails json ruby sqlite activerecord


【解决方案1】:

据我所知,如果您想在 SQLite3 中执行此操作,您需要在模型中设置 serialization。但是,在该页面上它提到,

请记住,数据库适配器会为您处理某些序列化任务。例如:PostgreSQL 中的 json 和 jsonb 类型将在 JSON 对象/数组语法和 Ruby Hash 或 Array 对象之间透明地转换。在这种情况下不需要使用序列化。

所以我想知道您是否打算使用 Postgres,它看起来可能有 json method

【讨论】:

  • 哈哈你说得对!我通常用 --database=postgresql 开始每个项目,这次一定忘记了。现在只需要改回来。谢谢!
  • @Alec 如果您几乎总是希望该命令行选项运行rails new --help 在描述中提到“您可以指定每次在.rails new 中运行时使用的额外命令行参数。 railsrc 配置文件在您的主目录中。”。从未做过,但似乎有助于意外忘记选项
猜你喜欢
  • 1970-01-01
  • 2013-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
  • 2015-03-16
  • 2021-09-24
相关资源
最近更新 更多