Rails,脏对象导致回滚

时间:2011-07-18 21:57:34

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

我有以下内容:

class User < ActiveRecord::Base

  before_update :guest_upgrade

  def guest_upgrade
    # If the user changed their email that means they were a guest, and are no longer.
    # Likely triggered from the Registrations#Update controller
    if self.email_changed?
      self.guest = false
    end
  end

这导致回滚,这里是带有上述内容的日志:

Started POST "/users" for 127.0.0.1 at 2011-07-18 14:54:00 -0700
  Processing by RegistrationsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"x+u1DSDanU2QXK/q0=", "user"=>{"fname"=>"xxxxx", "lname"=>"xxxx", "email"=>"xxxxxxx@gmail.com", "password"=>"[FILTERED]", "remember_me"=>"1"}, "commit"=>"Create my account", "fb_access_token"=>"", "fb_uuid"=>""}
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
  SQL (0.1ms)  BEGIN
  User Load (0.5ms)  SELECT "users"."id" FROM "users" WHERE ("users"."email" = 'xxxxxxx@gmail.com') AND ("users".id <> 5) LIMIT 1
  SQL (0.2ms)  ROLLBACK
  Authentication Load (0.5ms)  SELECT "authentications".* FROM "authentications" WHERE "authentications"."provider" = 'facebook' AND ("authentications".user_id = 5) LIMIT 1
Rendered layouts/_header.html.erb (3.9ms)
  CACHE (0.0ms)  SELECT "authentications".* FROM "authentications" WHERE "authentications"."provider" = 'facebook' AND ("authentications".user_id = 5) LIMIT 1
Rendered registrations/edit.html.erb within layouts/application (107.0ms)
Completed 200 OK in 484ms (Views: 111.7ms | ActiveRecord: 6.0ms)

然而,如果我注释掉guest_upgrade,它可以正常工作:

Started POST "/users" for 127.0.0.1 at 2011-07-18 14:55:23 -0700
  Processing by RegistrationsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxxxx+u1DSDanU2QXK/q0=", "user"=>{"fname"=>"XXXX", "lname"=>"XXXX", "email"=>"xxxx@gmail.com"}, "commit"=>"Save Changes"}
  User Load (1.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
  SQL (0.1ms)  BEGIN
  User Load (0.4ms)  SELECT "users"."id" FROM "users" WHERE ("users"."email" = 'xxxx@gmail.com') AND ("users".id <> 5) LIMIT 1
  AREL (0.7ms)  UPDATE "users" SET "fname" = 'XXXX', "lname" = 'XXXX', "email" = 'xxxx@gmail.com', "updated_at" = '2011-07-18 21:55:23.817142' WHERE "users"."id" = 5
[paperclip] Saving attachments.
  SQL (37.9ms)  COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 780ms

我是否错误地使用了脏物?

我想要做的就是,当用户更新时,如果用户更改了电子邮件,请将user.guest字段更改为false。

连连呢?感谢

1 个答案:

答案 0 :(得分:0)

我想我以前遇到过这个问题。我相信正在发生的事情是它正在返回虚假;尝试添加如下所示的显式返回:

if self.email_changed?
  self.guest = false
  return true
end

ruby on rails.org api查看标记为取消回拨的部分。它写着:

  

取消回调

     

如果before_ *回调返回false,则所有后来的回调和   相关操作已取消。如果after_ *回调返回false,   所有后来的回调都被取消了。回调通常在运行   它们的定义顺序,但定义为的回调除外   模型上的方法,最后称为。

我知道我迟到但我希望这会有所帮助!