ruby on rails - 2 way delete relationship -
i have user_relations
table.
user_id | related_user_id ------------------------- 1 | 15 1 | 17 4 | 56 15 | 1 5 | 34
when destroy row (1 | 15)
, want rails automatically delete parallel row (15 | 1)
.
is there rails way of doing this?
this user_relation class:
class userrelation < activerecord::base belongs_to :user, :class_name => "user", :foreign_key => "user_id" belongs_to :related_user, :class_name => "user", :foreign_key => "related_user_id" end
yes, can write filter in userrelation model,
after_destroy :delete_associated def delete_associated ur = userrelations.find_by_related_user_id_and_user_id(related_user_id, user_id) ur.delete if ur end
update:
to create associated record can write filter this,
after_create :create_associated def create_associated userrelations.find_or_create_by_related_user_id_and_user_id(related_user_id, user_id) #check if exist or create new end
Comments
Post a Comment