在Rails中将数组保存到postgres会引发ArgumentError:参数数量错误(3表示2)

时间:2013-11-27 04:06:25

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

将Rails 4.0.1与postgres一起使用我将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)

1 个答案:

答案 0 :(得分:0)

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