尽管未回滚,但属性未更新

时间:2019-04-22 10:50:42

标签: ruby-on-rails activerecord

我在Rails应用程序中更新了一个属性(“ estado”),没有错误消息。没有回滚,并且update方法的响应甚至是正确的。当我检查记录时,该属性具有旧值。

[1] pry(#<Users::RegistrationsController>)> resource
=> #<User id: 18, email: "password2@mail.com", created_at: "2019-04-22 10:37:55", updated_at: "2019-04-22 10:37:55", shop_id: nil, seller: false, first_name: nil, last_name: nil, birthday: nil, nationality: nil, document_type: nil, document_number: nil, expiring_date: nil, estado: "pending", admin: false, provider: nil, uid: nil, facebook_picture_url: nil, token: nil, token_expiry: nil>

[2] pry(#<Users::RegistrationsController>)> resource.estado = "Accepted"
=> "Accepted"

[3] pry(#<Users::RegistrationsController>)> resource
=> #<User id: 18, email: "password2@mail.com", created_at: "2019-04-22 10:37:55", updated_at: "2019-04-22 10:37:55", shop_id: nil, seller: false, first_name: nil, last_name: nil, birthday: nil, nationality: nil, document_type: nil, document_number: nil, expiring_date: nil, estado: "pending", admin: false, provider: nil, uid: nil, facebook_picture_url: nil, token: nil, token_expiry: nil>

[4] pry(#<Users::RegistrationsController>)> resource.save
   (0.3ms)  BEGIN
  ↳ (pry):27
  User Exists (0.8ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 AND "users"."id" != $2 LIMIT $3  [["email", "password2@mail.com"], ["id", 18], ["LIMIT", 1]]
  ↳ (pry):27
  User Update (2.3ms)  UPDATE "users" SET "encrypted_password" = $1, "updated_at" = $2 WHERE "users"."id" = $3  [["encrypted_password", "$2a$11$Pa2Dw8s53F8JSiO617Brxu2zuogK/Mffecokx0iKPnNJkVj05ZqsO"], ["updated_at", "2019-04-22 10:45:50.987192"], ["id", 18]]
  ↳ (pry):27
   (6.0ms)  COMMIT
  ↳ (pry):27
=> true

[5] pry(#<Users::RegistrationsController>)> resource
=> #<User id: 18, email: "password2@mail.com", created_at: "2019-04-22 10:37:55", updated_at: "2019-04-22 10:45:50", shop_id: nil, seller: false, first_name: nil, last_name: nil, birthday: nil, nationality: nil, document_type: nil, document_number: nil, expiring_date: nil, estado: "pending", admin: false, provider: nil, uid: nil, facebook_picture_url: nil, token: nil, token_expiry: nil>

[6] pry(#<Users::RegistrationsController>)> 

1 个答案:

答案 0 :(得分:0)

您的属性不会在第2步的实例中更新(见3),这意味着setter出了点问题。

这可能是由于:

  1. attr_accessor / attr_writer for :estado
  2. 自定义设置程序estado=未调用super(new_value)