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)