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

Sunday, July 20, 2008

Rails Console Tips & Tricks

Rails console have many useful tips that you may exploit as

Getting out of a block of code

If you're in an indented block of code (e.g., something that should be terminated with end) and want to escape without finishing, you can use ctrl + c to get back to the top level. The current expression will be ignored and not run.

Accessing the last return value

you can use the magic variable (_) for accessing last value
and you can then assign this value to a variable as x = _

Rollback all database changes on exit

script/console --sandbox