To learn more about why rails is so different from many other web application frameworks and paradigms examine the rails doctrine.
Ruby on rails has many.
A has many through association is often used to set up a many to many connection with another model.
2 4 the has many through association.
With has and belongs to many you don t need a primary key and you access the records through activerecord.
Rails tip i am using the shortened version of the rails generate command which is simply rails g.
Keep up to date with rails on twitter and this week in rails.
Earlier versions are not supported.
We will use the entity.
To do this as a one to many remove the parentalrelation model and table and update your customer class to something like this.
For example consider a medical practice where patients make appointments to see physicians.
See also activerecord associations classmethods s overview on when to use has one and when to use belongs to.
Has many and belongs to relationships made sense but the addition to many to many has many through relationships are where it got weird i ran into several issues while building my app and here s how i solved them.
This method should only be used if this class contains the foreign key.
Has many through and has and belongs to many relationships function through a join table which is an intermediate table that represents the relationship between other tables unlike a join query data is actually stored in a table.
The first step to implementing a has many through association is to run the rails generator to create the model and the migration files.
Methods will be added for retrieval and query for a single associated.
This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding through a third model.
Given you have customer has one parent in your model it looks like you are trying to create a one to many relationship.
The has many through and the has and belongs to many.
If the other class contains the foreign key then you should use has one instead.
With has many through associations this option is ignored at least in versions up to 2 1 0 so even if you set dependent option to destroy your join objects will be deleted not firing any callbacks you have set on destroy events.
Working with associations was the most difficult part of building central perk my ruby on rails application.
You only need a join table if you are creating a many to many relationship.
Ruby on rails 3 2 is the last series of releases that support ruby 1 8 7.
Specifies a one to one association with another class.