I have 2 models as describes below.
class EmpGroup < ActiveRecord::Base
belongs_to :user
has_many :emp_group_members, dependent: :destroy
end
and
class EmpGroupMember < ActiveRecord::Base
belongs_to :emp_group
belongs_to :user
end
now the problem is whenever I tried to destroy a group then I received a error as below.
PG::ForeignKeyViolation: ERROR: update or delete on table "emp_groups" violates foreign key constraint "fk_rails_bd68440021" on table "emp_group_members"
DETAIL: Key (id)=(1) is still referenced from table "emp_group_members".
What I'm missing?
When you delete a group, are you using delete or destroy. - I've had this error before, and it was because I had a typo and was using .delete instead of .destroy
:dependent is one of the options available in belongs_to association
Additionally, objects will be destroyed if they're associated with dependent: :destroy, and deleted if they're associated with dependent:
:delete_all.in has_many associations:
you can try
Add cascading delete to your EmpGroup model:
Or
Are you calling
delete
method? you should calldestroy
instead. Use.destroy