【问题标题】:Saving array to postgres in Rails raises ArgumentError: wrong number of arguments (3 for 2)在 Rails 中将数组保存到 postgres 会引发 ArgumentError: wrong number of arguments (3 for 2)
【发布时间】:2013-12-12 13:50:42
【问题描述】:

使用带有 postgres 的 Rails 4.0.1 我有一个存储为数组的 activerecord 列

create_table "accounts", force: true do |t|
  t.string "schedule_days", default: [], array: true
end

我可以分配一个数组就好了。

agent.schedule_days = agent.schedule_days << 1
 => [1] 

但保存不会持续。

agent = Account.last
agent.save
BEGIN
COMMIT

一些论坛建议 ActiveRecord 需要将列弄脏,所以:

agent.schedule_days_will_change!
 => [1] 

这会导致 SQL 语句发生更改,但会引发 ArgumentError。

agent.save
(0.3ms)  BEGIN
SQL (0.9ms)  UPDATE "accounts" SET "schedule_days" = $1, "updated_at" = $2 
WHERE "accounts"."type" IN ('Ghost') AND "accounts"."id" = 6543  
[["schedule_days", [1]], ["updated_at", Wed, 27 Nov 2013 03:56:14 UTC +00:00]]
ArgumentError: wrong number of arguments (3 for 2): UPDATE "accounts" SET "schedule_days" = $1, "updated_at" = $2 WHERE "accounts"."type" IN ('Ghost') AND "accounts"."id" = 6543
(0.3ms)  ROLLBACK 
ArgumentError: wrong number of arguments (3 for 2)

【问题讨论】:

  • 原来是一个过时的 activerecord 适配器导致了这个问题。

标签: arrays postgresql activerecord ruby-on-rails-4


【解决方案1】:

这是因为开始使用 activerecord-postgis-adapter 而不是 Rails 提供的默认 postgresql 适配器。可能有支持数组的activerecord-postgis-adapter 的更新版本。

【讨论】:

  • 太棒了!从 0.6.3 更新到 0.6.5,它可以工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多