Ben Johnson
2006-Jul-12 19:17 UTC
[Rails] Weird problem. How to tell if an attribute has changed? Please help.
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 Johnson E: bjohnson@contuitive.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060712/bd2f96b7/attachment.html
Richard Haven
2006-Jul-13 03:25 UTC
[Rails] Re: Weird problem. How to tell if an attribute has changed?
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 Johnson 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 -- Posted via http://www.ruby-forum.com/.
Peter Harkins
2006-Jul-13 03:40 UTC
[Rails] Weird problem. How to tell if an attribute has changed? Please help.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Wed, Jul 12, 2006 at 02:17:32PM -0500, Ben Johnson 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 Harkins - 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-----