Tuesday, August 4, 2009

Usefulness of named scope in remote console

Fancy searching through records in production database in a SSH terminal? You can always query them via good o' SQL but I find that rather cumbersome.

select * from payment_transactions outer left join utility_providers ..... where .....

Too long... what about activerecord objects using script/console?

PaymentTransaction.find(:all, :conditions => [........])

Better but the conditions string can get unweldy. But why not use namedscopes?

PaymentTransaction.vendor('Singtel').amount('30.00').account('123456').date_less_than('2009-08-01')

Short and sweet! Plus if you use the Searchlogic gem, these namedscopes comes for free! :)

No comments:

Blog Archive