如何覆盖find_by方法然后在activerecord中调用原始方法

时间:2014-06-13 15:39:32

标签: ruby-on-rails rails-activerecord

我想覆盖模型中的find_by_方法... 然后在里面我想调用原始+一些额外的代码。

class User < Activerecord::Base
  def self.find_by_email(email)
    binding.pry
    super.find_by_email(email) || find_by_social_identities_email(email)
  end
...

end

欢迎任何想法????

2 个答案:

答案 0 :(得分:3)

最后我这样做了:

class User < Activerecord::Base
  def self.find_by_email(email)
    super.find_by('email = :email', {email: email}) || find_by_social_identities_email(email)
  end
  ...
end

答案 1 :(得分:0)

对于Rails 5,我需要使用self而不是super。所以它变成了:

class User < ApplicationRecord
  def self.find_by_email(email)
    self.find_by('email = :email', {email: email}) || find_by_social_identities_email(email)
  end
  ...
end