ruby
When you’re debugging/analyzing MySQL queries in the Rails console, it helps to turn on ActiveRecord logging:
#Enable ActiveRecord logging def loud_logger(enable = true) logger = (enable == true ? Logger.new(STDOUT) : nil) ActiveRecord::Base.logger = logger ActiveRecord::Base.clear_active_connections! end
collect {|item|block} and map{|item|block} do the same thing – return an array of things returned by the block. This is different from returning specific items in the collection being iterated over.
Which leads to select.
select{|item|block} will return actual collection items being iterated over if, for each item, the block condition evaluates to true. Not the same as returning what the block, itself, may return. In the case of select, the block would always return an instance of class TrueClass or FalseClass. Typically, [true, false, ..., true] is not what you’re looking for in your resulting array.
Slightly modifying the core RDoc example:
a = ["a", "b", "c", "d"] #=> ["a", "b", "c", "d"]
a.map {|item|"a" == item} #=> [true, false, false, false]
a.select {|item|"a" == item} #=> ["a"]
I use the Rails console mainly to poke around in my database. Unfortunately the display of the records returned leaves a lot to be desired. Hirb solves this problem perfectly! Here are the quick steps you need to get the basic functionaliy:
- Install the gem: sudo gem install cldwalker-hirb –source http://gems.github.com
- Start the console: ruby script/console
- Require Hirb: require ‘hirb’
- Enable it: Hirb.enable
- Try it: x = Model.find(:all)