Tuesday, December 27, 2011

Refreshing the column in rails

It may be possible that your database is shared with multiple applications and you data are frequently changing. You can manually refresh the object by:

stock = Stock.find_all
loop do
puts “price = #{price}”
sleep 60

Monday, December 19, 2011

Rails Observers vs Callbacks

Rails Callback

Callbacks are methods that get called at certain moments of an object’s life cycle. That means that it can be called when an object is created, saved, updated, deleted, validated, or loaded from the database.

A callback is more short lived. And you pass it into a function to be called once. 

Rails Observers

Observer is similer to Callback but it is used when method is not directly related to object lifecycle. The other difference is an observer lives longer and it can be attached/detached at any time. There can be many observers for the same thing and they can have different lifetimes. 
The best example of Observer is that it could be “send registration confirmation emails” As we can argued that a User model should not include code to send registration confirmation emails so we will create observer for this.