Tuesday, July 22, 2008

acts_as_modified plugin

is it changed? which attributes? what are the old values? in some cases you may be asking yourself these questions when dealing with some models

When should you care about these questions?

suppose you have a model doing an expensive operation on save. this operation is useful only if one specific attribute changed. so doing it on every save neglecting which attributes changed will degrades performance significantly

So what can you do?

you can load model old data from database and check if that attribute has changed or not and if so do your action


you can use acts_as_modified plugin, it helps keeping modified attributes with their old values within model in order to be capable of answering your questions without doing any extra database trips

I am exited, from where can i get it?

it depends on your rails version

1. ((rails 2.1)) do nothing, dirty objects already embedded in this version
2. ((rails r7315 and above)) install from http://svn.viney.net.nz/things/branches/acts_as_modified_edge
3. ((ELSE)) install from http://svn.viney.net.nz/things/rails/plugins/acts_as_modified

That's it, Enjoy the fun

No comments: