views:

43

answers:

2

I dont understand, nothing could be simpler:

class Visit < ActiveRecord::Base
  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?

A: 

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.

Slobodan Kovacevic
Sorry, I did these tests already. visited_on is a :string (I know it is stupid but it is the only way I have found to be able to use Visit.count(:all, :group=>:visited_on) to get results grouped by day.And yes, I did change the user (or any other attributes) to random values before trying to save. I can see in the log that the save is done, but the before_filter is not executed. Very weird!
Alain
A: 

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.

Alain