Prev | Current Page 112 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

Rather, a normal delete_all is performed. This means
that any destroy callbacks on the children will not be executed.
Completely Nondynamic Finders
It should be mentioned that Active Record, in the end, works by passing an SQL string to the
database through an adapter. Since Active Record doesn??™t deny its roots, you are also able to
access the database in a more direct fashion. To this end, Active Record supplies a find_by_sql
method as well as the execute method.
You can use the find_by_sql method to pass in a string of SQL and obtain an object that
contains the attributes you specify in the SELECT clause. The useful thing about find_by_sql is
CHAPTER 2 ?–  ACTIVE RECORD AND SQL 37
that Active Record will still return an array of objects, saving you the tedium of having to parse
through everything yourself. The type of object that gets returned is the same as the class that
find_by_sql was called on. That this means if you call
Song.find_by_sql("SELECT users.* FROM users")
you??™ll have an array of Song objects that contain User data.


Pages:
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124