Rails更新帖子中相同类型的多个模型

时间:2011-02-10 11:10:49

标签: ruby-on-rails ruby-on-rails-3

我有一个问题,我试图更新帖子中相同类型的多个模型。循环中的第一个模型得到更新,但其余的不是出于某种原因。

我已经发布了服务器日志,您可以在其中查看并更新第一个模型,然后查找其余模型。

有没有人对我的问题有什么想法?感谢

  def special_save
    temp = params["_json"]

    temp.each { |h|
      hh = h['h_w']

      h_w = HourWorking.find(hh[:id])

      hh.delete('id')

      h_w.update_attributes(hh)
    }
  end   



HourWorking Load (2.5ms)  SELECT "hour_workings".* FROM "hour_workings" WHERE ("hour_workings"."id" = 187) LIMIT 1
SQL (1.2ms)  BEGIN
SQL (3.7ms)   SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"hour_workings"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
SQL (1.7ms)  UPDATE "hour_workings" SET "end_time" = '2000-01-01 04:45:00.000000', "start_time" = '2000-01-01 01:45:00.000000', "updated_at" = '2011-02-10 10:18:19.955198' WHERE ("hour_workings"."id" = 187)
SQL (4.3ms)  COMMIT
HourWorking Load (1.7ms)  SELECT "hour_workings".* FROM "hour_workings" WHERE ("hour_workings"."id" = 169) LIMIT 1
SQL (1.2ms)  BEGIN
SQL (12.8ms)  COMMIT
HourWorking Load (6.0ms)  SELECT "hour_workings".* FROM "hour_workings" WHERE ("hour_workings"."id" = 154) LIMIT 1
SQL (1.5ms)  BEGIN
SQL (1.3ms)  COMMIT
HourWorking Load (2.3ms)  SELECT "hour_workings".* FROM "hour_workings" WHERE ("hour_workings"."id" = 170) LIMIT 1
SQL (9.8ms)  BEGIN
SQL (1.1ms)  COMMIT
HourWorking Load (1.5ms)  SELECT "hour_workings".* FROM "hour_workings" WHERE ("hour_workings"."id" = 171) LIMIT 1
SQL (1.2ms)  BEGIN
SQL (12.3ms)  COMMIT
HourWorking Load (6.2ms)  SELECT "hour_workings".* FROM "hour_workings" WHERE ("hour_workings"."id" = 165) LIMIT 1
SQL (1.1ms)  BEGIN
SQL (1.6ms)  COMMIT
HourWorking Load (2.0ms)  SELECT "hour_workings".* FROM "hour_workings" WHERE ("hour_workings"."id" = 153) LIMIT 1
SQL (1.0ms)  BEGIN
SQL (1.6ms)  COMMIT

2 个答案:

答案 0 :(得分:1)

尝试删除:id键

hh.delete(:id)

答案 1 :(得分:0)

啊我发现了我的问题。

我遇到的问题是,我发布的params是数据库中已有内容的精确副本。