Brad Ediger
"Advanced Rails"
find_by_name 'Og'
=> #
The find_with_attributes method (an alias for query_by_example) has some nice syntactic
sugar to provide a ???pattern??? to match the searched-for attributes. We can view
the SQL queries being generated by setting the $DBG global variable to true. For
example:
>> $DBG = true
=> true
>> TodoList.find_with_attributes :name => 'Og'
DEBUG: SELECT * FROM ogtodolist WHERE name = 'Og'
=> [#]
>> TodoList.find_with_attributes :name => 'O%'
DEBUG: SELECT * FROM ogtodolist WHERE name LIKE 'O%'
=> [#]
Associations work very much like in ActiveRecord. The association contents are
lazily loaded; the collection is not loaded until something is done with the data.
>> list = TodoList[1]
DEBUG: SELECT * FROM ogtodolist WHERE oid=1
=> #
# Note that this statement issues no query.
>> list.todo_list_items
=> #, oid1, position1
# Sending the collection a message such as #to_a or #map forces it to load.
>> list.todo_list_items.to_a
DEBUG: SELECT * FROM ogtodolistitem WHERE todo_list_oid = 1
=> [#@name="Make Og work with ActiveSupport's Dependencies",
@todo_list_oid=1, @oid=1, @position=1>,
#@name="Autoload Orderable so that Ruby can find it",
@todo_list_oid=1, @oid=2, @position=2>]
>> list.
Pages:
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428