Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1

280 | Chapter 9: Incorporating and Extending Rails


To get a better idea of the queries being issued, it is helpful to redirect
the Rails logger to standard output when using the Rails console. This
code, thanks to Chad Humphries and Tim Lucas, will redirect Rails
log activity to the console. Put this block in your~/.irbrc.
script_console_running = ENV.include?('RAILS_ENV') &&
IRB.conf[:LOAD_MODULES] &&
IRB.conf[:LOAD_MODULES].include?('console_with_helpers')
rails_running = ENV.include?('RAILS_ENV') &&
!(IRB.conf[:LOAD_MODULES] &&
IRB.conf[:LOAD_MODULES].include?('console_with_helpers'))
irb_standalone_running = !script_console_running &&
!rails_running
if script_console_running
require 'logger'
Object.const_set(:RAILS_DEFAULT_LOGGER, Logger.new(STDOUT))
end

The first run ofscript/console creates the SQLite database file and our tables.


$ script/console
Loading development environment (Rails 1.2.5)
INFO: Og uses the Sqlite store.
INFO: Created table ogtodolistitem.
INFO: Created table ogtodolist.
INFO: Created table ogtag.
>>

All Og-managed tables currently start withogand are simply the downcased, alpha-
numeric versions of the class names. The primary key defaults tooid, notid. The
ogtagtable was created because the optional tagging module was loaded; it currently
creates a table whether it is in use or not.


Now we can create a to-do list and some items:


>> og = TodoList.create_with :name => 'Og', :position => 1
=> #<TodoList:0x33e75bc @validation_errors={}, @name="Og", @oid=1, @position=1>

>> item1 = TodoListItem.create_with \
:name => "Make Og work with ActiveSupport's Dependencies",
:position => 1, :todo_list => og
=> #<TodoListItem:0x33dd3b4 @validation_errors={}, @todo_list_oid=1,
@name="Make Og work with ActiveSupport's Dependencies", @position=1,
@oid=1>

>> item2 = TodoListItem.create_with \
:name => "Autoload Orderable so that Ruby can find it",
:position => 2, :todo_list => og
=> #<TodoListItem:0x33ce1c0 @validation_errors={}, @todo_list_oid=1,
@name="Autoload Orderable so that Ruby can find it", @position=2,
@oid=2>
Free download pdf