builder interview preparation
polymorphic association :: picture product employee
has_and_belongs_to_many :: assemblies parts
has_many_through association patients physicians appointments
has one_through association supplier account and account history
self join employee with its subordinate and manager
Polymorphic Association
.....................................
Class Picture < Application Record
belongs_to :imageable,polymorphic: true
end
Class Product <Application Record
has_many :pictures ,as: :imageable
end
Class Employee< Application Record
has_many :pictures ,as: :imageable
end
has_and_belongs_to_many
.....................................
Class Assembly < Application Record
has_and_belongs_to_many :parts
end
Class Part <Application Record
has_and_belongs_to_many :assemblies
end
The has_many :through Assocication :: many to many association
.....................................
this association is often used to set up a many -to-many connection with another model , this association indicates that the declaring model can be matched with zero or more instances of another model by proceeding through a third model
patient appointment physician
Class Patient < Application Record
has_many :appointments
has_many :physicians,through: :appointments
end
Class Appointment < Application Record
belongs_to :patients
belongs_to :physicians
end
Class Physician < Application Record
has_many :appointments
has_many :patients,through: :appointments
end
....................................................................................................
The has_one :through Assocication :: one to one association
.....................................
A has one thorugh association sets up a one to one connection with another model. this association indicates that the declaring model can be matched with one instance of another model by procceeding through third model
Ex:: this example is with supplier , account and account history
supplier account account-history
if each supplier has one account and each account has one account history then the supplier model could look like this ::
Class Supplier < Application Record
has_one :account
has_one :accounthistory,through: :account
end
Class Account < Application Record
belongs_to:supplier
has_one:account_history
end
Class AccountHistory < Application Record
belongs_to:account
end
Self Joins ::
sometimes in desigining a data model we find a model that should have a relation to itself
Self join is the join method in which a table makes the relation with itself in this a table creates its two copies and one table use the foreign key as the primary key with the other table.
For ex :: when we have the employee table and we have the manager and subordinates then we can make the two copies of the table and makes the relationship between manager and subordinates as employee with the help of self join method of the association.
...........................................................
Class Employee < Application Record
has_many :subordinates, class_name : "Employee"
foreign_key : "manager_id"
belongs_to :manager, class_name: "Employee", optional:true
end
..........................................
In this we use the class_name and foreign key :: in this we take the class_name as the table name and we take the foreign key inside has_many association.
we can retreive ::
@employee.subordinates and @employee.manager
Comments
Post a Comment