SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 182 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

farmer_id = cows.farmer_id
where farmer_id = ?",1])
cows.each do |rec|
print rec.cowname + ", "
end
# => Bessie, Spotty,
The Active Record equivalent should look something like the following (again, regardless
of which version of SQL your database supports):
fred = Farmer.find(1)
fred.cows each do |cow|
print cow.name + ", "
end
# => Bessie, Spotty,
As you can see, with this simple association, you automatically get the collection of associated
records from the associated table (cow) and have access to all the attributes of those
records (like the cows??™ names).
has_one
Sometimes, you don??™t have a collection of items to associate; sometimes, you have just one.
In this case, has_one is what you need. Basically, has_one is the same as has_many, but Active
Record will return only one object instead of all of them.
To be honest with you, it should be a rare case that you find yourself using the has_one
association, because you would more likely just add the fields to one master table rather than
split them across two tables and create a one-to-one relationship.


Pages:
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
hotel jelenia góra Russian bride Free English grammar and study guid powiekszenia wielkoformatowe counter strike 1.6