Old MacDonald had a farm
One of the things that has been most complicated for me are the ActiveRecord associations when using Rails. I was talking with my teammate Sterling about this today, and she used an excellent metaphor of a farmer. So, this blog post will expand on that. I'm going to attempt to explain the belongs_to has_many relationship, and the has_one belongs_to association. Hopefully this will prevent confusion in the future.
So, Old MacDonald has a farm.
And on this farm he has animals, such as cow, a duck, a sheep and a dog. If we are representing the local farms in this app, in our model the cows would be farm/app/models/cow.rb.
And, our farmer model is represented here at farm/app/models/farmer.rb. I'm only looking at one farmer that there is only one farmer on this farm, though old MacDonald could have a family or live in a commune with other farmers. The model supports having multiple farmers.
Let's look at the cows, understanding that Old MacDonald can have other animals, but their database representation will follow this same pattern. Old MacDonald has many cows, and each cow belongs only to Old MacDonald. So, Old MacDonald has_many cows (plural), and the cows(plural) belong_to Old MacDonald. In the database, the class that is belonging to another class would have a column with a foreign key to represent this relationship. so the cow model will hold the relationship with Old MacDonald. So our migration at farm/app/db/20140608/_create_cows would look something like the following:
As you can see, there is a column with the belongs_association in the migration. This is a one-to-many type of association. Farmers can have many cows, but each cow can only belong to one farmer. The has_many and belongs_two associations are somewhat paired. My farmer must have_many (though he could only have one or zero, depending on what kind of farmer he is), cows, while the cows must belong_to a single farmer.
Now onto the has_one type of association. This type of association sets up a one-to-one relationship between models. Both elements or classes must have exactly one instance of the other class. On a sidenote, the has_many type of association returns an array of Active Record objects when queried by the database. This next type of association, the has_one, returns a single ActiveRecord object when queried by the database.
Let's give our cows a brand. The would be a has one association, since each cow can only fit one brand on their body. Editing our Cow model at farm/app/models/cow.rb, we would have something like:
As you can see in the code, our cow has_one brand. Now, if we were to keep track of the brands, the brand belong_to a cow, setting up this one:one relationship. In our code, the model in farm/app/models/brand.rb would be represented as
Like the cows table, in our database, the brands would have a column with the cow_id to show which cow they belong to. The migration would look something like the following
As you can see, just like in the cow table, the brands table have a belongs_to column that would hold the ID of the cow. One of the nice things that ActiveRecord does is increment the ID's of the items in your table once you create a database, so the column for cow_id would be automatically generated by some awesome Rails magic.
<script src="https://gist.github.com/ajosepha/8709589.js"></script>And there you have it! Let's go eat a steak!









