Prev | Current Page 110 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

new(:first_name => "Bob", :last_name => "McCracken")
pm = Person.new(:first_name => "Pam", :last_name => "McCracken")
jm = Person.new(:first_name => "Joey", :last_name => "McCracken")
bm.children << pm
bm.children << jm
bm.save
This code executes the corresponding SQL statements:
BEGIN;
INSERT
INTO people (`first_name`, `last_name`, `parent_id`)
VALUES ('Bob', 'McCracken', 0);
INSERT
INTO people (`first_name`, `last_name`, `parent_id`)
VALUES ('Pam', 'McCracken', 1);
INSERT
INTO people (`first_name`, `last_name`, `parent_id`)
VALUES ('Joey', 'McCracken', 1);
COMMIT;
CHAPTER 2 ?–  ACTIVE RECORD AND SQL 36
Afterward, the people table would have the following entries:
+----+-----------+------------+-----------+
| id | parent_id | first_name | last_name |
+----+-----------+------------+-----------+
| 1 | 0 | Bob | McCracken |
| 2 | 1 | Pam | McCracken |
| 3 | 1 | Joey | McCracken |
+----+-----------+------------+-----------+
Now, if you subsequently execute the following code
bm = People.find_by_first_name_and_last_name("Bob", "McCracken")
bm.


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