Quantcast
Viewing all articles
Browse latest Browse all 5

Using belongs_to to handle multiple Parent Records in a single child record

This is a very small post where I shall explain some additional features of belongs_to which I myself didn’t know about until I saw a requirement. Generally belongs_to in common sense is used along with has_one or has_many associations to associate relationships between 2 tables where user can use the information.

So in the basic sense, whenever there is a need to connect Parent and Child tables with belongs_to and has_many relationship, developers generally use the following known table relationship i.e. Database Information.

1. Create the Parents table with ID.

2. Create the Children table with ID.

3. Add a Field by name parent_id in children table Model Information.

In Parent model, add an association to the child model.

class Parent < ActiveRecord::Base
has_many
:children
end

In Child model, add an association to parent model.

class Child < ActiveRecord::Base
belongs_to
:parent
end

This is a basic idea of using a belongs_to and has_many relationships and solves most of the problems used in rails programming.

However, when there exists that multiple connectivity is required in a particular Parent-Child relationship, then this concept does not work. Considering the above example, the requirement says that for any child record in the children table, there can exist only one parent table. Sometimes there exists a requirement of more than one entry e.g. Mother and Father is a parent for a child. So when there is a requirement to have a child record created with 2 foreign keys of Parents table i.e. one for father_id and another for mother_id, the above concept does not work.

In this kind of relationship, the solution is to modify the above requirement as Database Information.

1. Create the Parents table with ID.

2. Create the Children table with ID.

3. Add 2 fields by name father_id and mother_id in the children table.

Model Information in Parent model adds an association to the child model.

class Parent < ActiveRecord::Base
has_many :children
end

Model information in Child model adds an association to parent model.

class Child < ActiveRecord::Base

belongs_to :father, :class_name => “Parent”, :foreign_key => “father_id”
belongs_to
:mother, :class_name => “Parent”, :foreign_key => “mother_id”

end

Another typical example to explain this need is in a shopping cart where you would have an order attached to 2 different addresses such as shipping address and billing address, where both are of type address. In the example, the subject of interest is using the belongs_to methodology. Let us investigate what the above sentence mean.

belongs_to :father, :class_name=>“Parent”, :foreign_key=>“father_id”

When we say belongs_to :father, we mean that you can use the keyword father to get information about the father object i.e.

@child = params[:selectedchild]
@father
= @child.father
@father…. #Get father details….

Since there does not exist any table as father, we explicitly instruct the Model to consider this Father object to be an entry in the Parent object (table) and could be accessed by the foreign_key father_id of the children table.

Hope this post explains how to solve multiple elements of a parent within a child record.

– Heurion Consulting Information Release


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 5

Trending Articles