Notes posted by GavinLaking

RSS feed
July 23, 2008
1 thank

Using .map(&:item)

You can only use .map(&:item) with find(:all, not find(:first. For example; the first works, but the second does not.

@person = Person.find(:all, :conditions => {
  :id => @person.id}, :select => "name").map(&:name)

@person = Person.find(:first, :conditions => {
  :id => @person.id}, :select => "name").map(&:name)
July 22, 2008
4 thanks


In your migration:

def self.up
  add_column :accounts, :is_admin, :boolean, :default => 0
July 3, 2008
5 thanks

Connections to multiple databases

establish_connection can be used to connect to multiple databases. The immediate downside is that your rake migrations may not work properly without hacking.

In each model that resides in a different database we call:

establish_connection :different_database

You can check they are working by hitting script/console with:

>> App.connection.instance_eval {@config[:database]}
=> "app_development"
>> AnotherDatabase.connection.instance_eval {@config[:database]}
=> "another_database"

Now doing a call to AnotherDatabase.find() will connect to the AnotherDatabase database and start returning results.

July 1, 2008
5 thanks

benchmark and silence methods

benchmark (title, log_level = Logger::DEBUG, use_silence = true) {|| …}

Log and benchmark the workings of a single block and silence whatever logging that may have happened inside it (unless use_silence is set to false).

The benchmark is only recorded if the current level of the logger matches the log_level, which makes it easy to include benchmarking statements in production software that will remain inexpensive because the benchmark will only be conducted if the log level is low enough.

silence () {|| …}

Silences the logger for the duration of the block. To silence the logger in your controller, simply use:

NameOfController.logger.silence do

# your code here...