Devise current_user weird behavior after upgrade t

2019-09-13 17:04发布

问题:

I have upgraded by project from ree-1.8.7 to 1.9.2p429.

I have an attribute in my devise model named silhouette_user_id. In views and occasionally in my ruby code, calling

current_user.silhouette_user_id

returns a different (and WRONG) value than

current_user[:silhouette_user_id]

which returns the right value.

Though I could search and replace to fix this, I'm worried that other attributes will behave the same way. This is a LARGE project and I really need to determine why this is happening.

Any ideas would be greatly appreciated.

回答1:

I solved this issue. During the upgrade I had tried to update how I was defining new methods for the User class. Unfortunately, I did it wrong. I used

  def define_new_method(key, value)
    self.class.send(:define_method, key.to_sym) do
       value
    end
  end

which actually changed the methods for the CLASS User rather than the instance for which it was intended. Changing to this:

  def define_new_method(key, value)
    define_singleton_method key.to_sym, lambda { value }
  end

resulted in the new methods being defined only for the instance intended and not for the current_user instance.