Execute Rails migration code from IRB console

Posted . Visible to the public.

When in need to fix DB schema (e.g. replay bits of migration manually) or just run some arbitrary SQL from inside the IRB Rails console one has to prefix the methods like add_column or select_values with ActiveRecord::Base.connection, its easire to invoke new IRB session then with ActiveRecord::Base.connection passed as context, e.g.

irb(main):001:0> irb ActiveRecord::Base.connection
irb#1(#<ActiveRecord::ConnectionAdapters::Mysql2Adapter:0xbc3fc994>):001:0> tables
=> ["users", ...]

The annoyingly long prompt can be also stripped by changing the IRB prompt mode, e.g.

irb#1(#<ActiveRecord::ConnectionAdapters::Mysql2Adapter:0xbc3fc994>):002:0> conf.prompt_mode = :SIMPLE
=> :SIMPLE
>>     
Ev Dolzhenko
Last edit
Posted by Ev Dolzhenko to Ev's deck (2013-11-04 17:20)