Prev | Current Page 176 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"


For some reason, associations??”or joins, as SQL refers to them??”cause a lot of confusion
for many developers. The basic idea is actually quite simple. You have two or more tables, and
each contains a set of data that is logically associated in some way. You want to grab a collection
or subset of that data and treat it as one set of data. Seems simple enough, right?
The confusion generally comes when you start to describe these connections as inner joins,
outer joins, left outer joins, and so forth. Oftentimes, expressing what you want to in plain English
is easy enough, ???Give me a list of cows owned by farmers who live in Ohio.??? Somehow, though,
translating that into proper SQL statement to return the correct set of data gets mucked up.
We know we want to join the cow and farmer tables, but do we want to do an inner or outer
join on the tables? Should it be a left or full join? How do we know if it??™s really returning just
the data we want without having to manually check all the results?
Farmers, Cows,Milk, and How They Relate
In an attempt to clear things up and to explain how Active Record tackles associations between
models, let??™s take an example to talk about and break down each specific situation.


Pages:
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188