Notes posted by GavinLaking
RSS feed![Default_avatar_30](https://www.gravatar.com/avatar/c9c47875936bed67032a12c047b468a8?default=http://apidock.com/images/default_avatar_30.png&size=30)
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)
![Default_avatar_30](https://www.gravatar.com/avatar/c9c47875936bed67032a12c047b468a8?default=http://apidock.com/images/default_avatar_30.png&size=30)
![Default_avatar_30](https://www.gravatar.com/avatar/c9c47875936bed67032a12c047b468a8?default=http://apidock.com/images/default_avatar_30.png&size=30)
Getting textfield values into "link_to_remote" via Javascript Prototype
Use Prototype to get the value of a text field via Javascript, to pass to the ‘link_to_remote’ helper using code similar to below:
link_to_remote 'Link Name', {:update => "foo", :url => {:controller => "bar", :action => "baz"}, :with => "'model[textfield]=' + $F('textfield_id')"}
![Default_avatar_30](https://www.gravatar.com/avatar/c9c47875936bed67032a12c047b468a8?default=http://apidock.com/images/default_avatar_30.png&size=30)
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.
![Default_avatar_30](https://www.gravatar.com/avatar/c9c47875936bed67032a12c047b468a8?default=http://apidock.com/images/default_avatar_30.png&size=30)
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...
end