Each type of value is converted in the same way. The
values are also escaped by the adapter so that errant quotation marks or comment sections do not accidentally
trip up the database and cause it to do something it wasn??™t meant to do.
Active Record also allows you to create multiple objects across associations. For example,
if you had the following object
class Person < ActiveRecord::Base
belongs_to :parent, :class_name => "Person"
has_many :children, :class_name => "Person", :foreign_key => :parent_id
end
you could perform the following actions to create a parent and its children all at once:
person_1 = Person.new :first_name => 'Bonnie', :last_name => 'Pytel'
person_2 = Person.new :first_name => 'Chad', :last_name => 'Pytel'
person_1.children << person_2
person_1.save
The preceding code will result in the following SQL statements, assuming that the first
record inserted will be given the id of 1:
INSERT
INTO people (`first_name`, `last_name`)
VALUES (`Bonnie`,`Pytel`);
INSERT
INTO people (`first_name`, `last_name`, `parent_id`)
VALUES (`Chad`, `Pytel`, 1);
CHAPTER 2 ?– ACTIVE RECORD AND SQL 26
Reading a Record
Once you??™ve saved your records to the database, you??™re going to need a way to get them back.
Pages:
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103