Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • difference between HAS_ONE and BELONGS_TO association in rails?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 776
    Comment on it

    Has_one and Belongs_to relationship sometime become confusing so the main difference can be explained with the help of example:
    1>Has_one :-

    class Department < ActiveRecord::Base
    has_one: employee
    end
    

    this means that employee table has a foreign key Department_id i.e the foreign key by the name of declaring model is present in the declared model.

    2> Belongs_to:-

    class Department < ActiveRecord::Base
    belongs_to:employee
    end
    

    this means that the Department table has a foreign key by the name of employee_id i.e. the foreign key by the name of the declared table is present in the declaring table by the name of the declared model.

 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: