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 181 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

This becomes more clear with
our example, so let??™s define our farmer model and show the association in action:
class Farmer < ActiveRecord::Base
has_many :cows
end
CHAPTER 4 ?–  CORE FEATURES OF ACTIVE RECORD 71
The has_many association returns an array of objects from the table containing the matching
foreign key from the table specified. Again, walking through a plain English version makes
this easier to understand, so let??™s use this association as follows: get a list of the cows a given
farmer owns.
Here, we are starting with the farmer table??”we know the farmer we want information about,
and we want to get a list of cows that this farmer owns. This is really a lot like our belongs_to
example, in that we just want to join the two tables together based on farmer_id. However,
this time, we are starting in the farmer table with just the farmer ID. With SQL, our inner join
would look like the following:
# this is the T-SQL version (MS SQL Server uses T-SQL)
cowlist = Farmer.find_by_sql(["Select cows.name as cowname from
farmer inner join cows on farmer.


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