Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Associations in Rails Model

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 661
    Comment on it

    Associations in Rails

    Associations are an important part of each Active Records. It makes coding easier and relations clean. Lets take an example of Blog. A Blog can have many comments. So if you delete blog its associated comments also needs to be deleted. So in these kinds of situations associations plays a major role.
    We can specify the relations between two models as:

    class Blog < ActiveRecord::Base
      has_many :comments, dependent: :destroy
    end
     
    class Comment < ActiveRecord::Base
      belongs_to :blog
    end
    

    By using this creation of a comment becomes easier as:

    @comment = @Blog.comments.create(text: "A helpful Blog")
    

    And deleting the Blog will delete all the associated comments.

    @blog.destroy
    

    Types Of Associations:
    Rails support six types of associations. Those are:

    (a) belongs_to : - It sets up one-to-one connection with another model. So that one instance of model can be related to only one instance of another model. Like a comment can be associated to only one Blog. So it will be declared as in previously given example:

    class Order < ActiveRecord::Base
      belongs_to :customer
    end
    

    (b) belongs_to : - It also sets up one-to-one connection with another model. The difference is that in this case each instance contains or possesses one instance of another model. As an example if we store the analytics of a blog in one table, then a blog can have only one analytics. So there relationship would be blog has_one analytic. It would be written in rails as:

    class Blog < ActiveRecord::Base
      has_one :analytic
    end
    

    Note: To check whether to use has_one and belongs.Check this out Choosing Between belongs_to and has_one

    (c) has_many : - It indicates one-to-many relationship between two models. It means one instance of a model can have 0 to many instance of another model. In our case a blog can have many comments. So there will be blog has_many comments at one end and on other end it would be comment belongs_to blog.In rails we will write as:

    class Blog < ActiveRecord::Base
      has_many :comments
    end
    

    (d) has_many :through : - It is used to set up many-to-many connection with another model. It represents that the declaring model can be matched to 0 or more model instance of another model via third model. As an example if a blog can have many comments and a user can have many blogs, then :

    class Blogger < ActiveRecord::Base
      has_many :blogs
      has_many :comments, through: :blogs
    end
     
    class Comment < ActiveRecord::Base
      belongs_to :blogger
      belongs_to :blog
    end
    

    (e) has_one :through : - It is used to set up one-to-one connection with another model. It represents that the declaring model can be matched to one instance of another model via third model. As an example, if a blog has one analytic and analytic has one analytic_history then blog can be related to analytic history via analytic. In rails we will write it as:

    class Blog < ActiveRecord::Base
      has_one :analytic
      has_one :analytic_history, through: :analytic
    end
     
    class Analytic < ActiveRecord::Base
      belongs_to :blog
      has_one :analytic_history
    end
     
    class AnalyticHistory < ActiveRecord::Base
      belongs_to :analytic
    end
    

    (f) has_and_belongs_to_many : - It indicates a direct many-to-many connection with no third model between them. For a example if a tutionclass has many teacher and students and each teacher can have many students and each student can study from many teachers. In rails we will write this relationship as:

    class TutionClass < ActiveRecord::Base
      has_and_belongs_to_many :students
    end
     
    class Student < ActiveRecord::Base
      has_and_belongs_to_many :teachers
    end
    


    I hope you liked associations in rails and my blog too. Some more topics are given below. Please give a look into them.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: