I understand the concept of relational databases, primary/foreign keys, etc, however, I'm having trouble seeing the actual result of setting these properties up in my models. Do they generate helper functions or something similar? or do they simply work on a database level?
For example, if I have these two models (other properties omitted)
class Course < ActiveRecord::Base
has_and_belongs_to_many :schedules
has_many :sections
end
class Section < ActiveRecord::Base
belongs_to :course
end
I could simply get all sections for any given course like this:
Section.where(course_id: 1234)
However, I could do this without having set up the relations at all. So my question is: Why do we do this?
Adding these methods let's you do things like this:
Also it let's you join your queries more easily:
Neither of these would be possible if you didn't set up the relations in the model.
Similarly:
It makes your calls more semantic, and makes things easier from a programmatic perspective :)