开发者

Before_save filter not working

开发者 https://www.devze.com 2023-01-02 13:12 出处:网络
I dont understand, nothing could be simpler: class Visit < ActiveRecord::Base开发者_Python百科

I dont understand, nothing could be simpler:

class Visit < ActiveRecord::Base开发者_Python百科
  def before_save
    self.visited_on = "test" 
  end
end

Yet, if I do:

a = Visit.first
a.user_id = 5
a.save
a.visited_on
=> nil #WTF?

I know that filters must return true, but this one is... What could be the issue?


Are you sure your record is actually being saved? If user_id is already 5 then a.save won't perform the save. Also if visited_on is date and/or time then "test" might be converted to nil. Try using current time or something similar.


Typical stupid dev error: I had two classes Visit in my code. I dont know how it happened but deleting the empty class fixed the issue. Sorry for wasting your time.

0

精彩评论

暂无评论...
验证码 换一张
取 消