The sqlconsole gem provides sql functionality from within the standard rails console. I'm a long time SQL user, and though I love ActiveRecord, I find myself frequently needing to perform sql statements to quickly update large quantities of rows, to query lookup tables and scan across multiple rows, etc. Tired of having to switch back and forth between dbconsole and the rails console, I created this.
You'll need to install the sqlconsole gem first gem install sqlconsole And then also the awesome print gem. At least for now, I am using that to render the sql result in a Hash format. gem install awesome_print Then, you can manually type in the following each time you launch your console, require 'sqlconsole' Or, a better option is to modify your .irbrc. For example, the block below shows the proper placement of the require command in customized .irbrc file. This sample block also turns on logging for SQL commands to show you the real SQL being executed. I find this helpful, but if you don't, remove the three logger lines. Note: The require for sqlconsole must be inside this block to ensure a) that this is running within a Rails environment, and b) that it runs after Rails has loaded, to ensure the database connection has been made.
# Some default enhancements/settings for IRB, based on # wiki.rubygarden.org/Ruby/page/show/Irb/TipsAndTricks if ENV['RAILS_ENV'] # Called after the irb session is initialized and Rails has been loaded IRB.conf[:IRB_RC] = Proc.new do logger = Logger.new(STDOUT) ActiveRecord::Base.logger = logger ActiveResource::Base.logger = logger require 'sqlconsole' end end
While you are editing your .irbrc file, I also recommend using the awesome_print gem in general. It makes it much easier to see the Hashes out put by model dumps in the console. To to this, modify your .irbrc file to include the following:
unless defined? ETC_IRBRC_LOADED
# Require RubyGems by default. require 'rubygems'
begin require “ap” IRB::Irb.class_eval do def output_value ap @context.last_value end end rescue LoadError => e puts “ap gem not found. Try typing 'gem install awesome_print' to get super-fancy output.” end end I've included my .irbrc file which sets up awesome print, tab completion, console history saving, and this gem, sqlconsole.
Fork the project.
Make your feature addition or bug fix.
Add tests for it. This is important so I don't break it in a future version unintentionally.
Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
Send me a pull request. Bonus points for topic branches.
Copyright © 2010 Brian Burridge. See LICENSE for details.