Weird problem. How to tell if an attribute has changed? Plea

How do you tell if an foreign key attribute has been changed?

For example, lets say you have a model with attribute table_a_id.
Would you do this?

def table_a_id=(value)
if value != table_a_id
@table_a_changed = true
write_attribute ‘table_a_id’, value
end
end

def table_a=(value)
if value != table_a
@table_a_changed = true
@table_a = value
end
end

I did this and when I try to save the model I get ‘undefined method
updated?’. What am I doing wrong here? Or is there a better way to
determine if an attribute has changed? The reason I’m doing this is
because in my after_save method I need to run some special code if
the relationship has changed.

Thanks a lot for your help.

Thank You,
Ben J.
E: [email protected]

Adding a side-effect to the mutators seems like the way to go: that’s
what mutators are for. I’m a little unsure about updating each attribute
immediately (which write_attribute() seems to do).

Also, where do you resent @table_a_changed ?

Are you sure it is not talking about a method named “updated?”. I’ve had
hours of fun working out what ruby error message are actually talking
about.

Ben J. wrote:

def table_a_id=(value)
if value != table_a_id
@table_a_changed = true
write_attribute ‘table_a_id’, value
end
end

I did this and when I try to save the model I get ‘undefined method
updated?’. What am I doing wrong here? Or is there a better way to
determine if an attribute has changed?

Cheers

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, Jul 12, 2006 at 02:17:32PM -0500, Ben J. wrote:

How do you tell if an foreign key attribute has been changed?

It sounds like the acts_as_modified plugin is what you want:
http://svn.viney.net.nz/things/rails/plugins/acts_as_modified/


Peter H. - http://push.cx

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: If you don’t know what this is, it’s OK to ignore it.

iD8DBQFEtcBpa6PWv6+ALKoRAoQyAJ9v0yRbAMuYdjPYLlsOuBEJhVnjWACfXvnP
YS4t9Cx1mnl9j2bUPM1zGqA=
=Kykz
-----END PGP SIGNATURE-----