activerecord - In rails build the relationship in the has_and_belongs_to_many association -
in rails app, have advisor model , department model. advisor has , belongs many departments , department has , belongs many advisors.
if advisor , department exist. want create relationship between them, put advisor_id , department_id in advisors_departments joint table.
advisor.departments.build(:id => @department_id)
doesn't work since department exist.
alternatively,it can through joint model implement many many relationship, work around it.
but chose use has_and_belongs_to, there easy way create association between 2 existed instances?
department = department.find(@department_id) advisor.departments << department
and ror should take care of rest : )
Comments
Post a Comment