Sunday, December 20, 2009

Find last updated date for ActiveRecord model

Often a page needs to show when content was last updated, its easy to do when showing a particular record, but what about the last updated date for a list of records?

This is a little snippet I use:

date_of_last_fee_change = Fee.last_updated

To make it happen for all models:

ActiveRecord::Base.class_eval do
  def self.last_updated
    last = first(:order => 'updated_at DESC')

Tuesday, August 11, 2009

Easily manage multiple apps

Sometimes you want to run a command on each of your rails apps. i.e. git pull or rake db:migrate

A simple ruby script can handle that:



PATH = '/mnt/apps'

Dir.glob("#{PATH}/*").each do |dir|
puts "(in #{dir})"
ARGV.each { |cmd| system(cmd) }

This script iterates each folder under "/mnt/apps" directory, goes into that directory and executes each command.

you can run it like so:

./each-app.rb 'git pull' 'rake db:migrate'

just dont forget to

chmod +x each-app.rb

happy hacking