In my rails model, I have a decimal property called employer_wcb. I would like it if, when employer_wcb was changed, a dirty bit was set to true. I'd like to override the employer_wcb setter method. Any way to do so (in particular using metaprogramming)?
ruby on rails add functionality to model property change
1k Views Asked by Daniel At
2
There are 2 best solutions below
1

If you don't want to use rail's built in dirty bit functionality (say you want to override for other reasons) you cannot use alias method (see my comments on Steve's entry above). You can however use calls to super to make it work.
def employer_wcb=(val)
# Set the dirty bit to true
dirty = true
super val
end
This works just fine.
So actually, as of Rails v2.1, this has been baked into Rails. Have a look at the documentation for ActiveRecord::Dirty. In summary: