Prev | Current Page 414 | Next

Brad Ediger

"Advanced Rails"

Here are some model classes for a very simple to-do list:
app/models/todo_list.rb
class TodoList
attr_accessor :name, String
# Orderable is like ActiveRecord's acts_as_list.
# Equivalent to:
# include Og::Mixin::Orderable
# or (as Og::Mixin is included at the top level)
# include Orderable
is Orderable
has_many :todo_list_items, TodoListItem
end
app/models/todo_list_item.rb
class TodoListItem
attr_accessor :name, String
belongs_to :todo_list, TodoList
is Orderable, :scope => :todo_list
end
Now we can open up the Rails console and start playing with the model classes we
have created.
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 of script/console creates the SQLite database file and our tables.


Pages:
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426