ruby - Rails create callback for 2 dependent resources -
i want know how can use create callback before_create or after_create 2 dependent models.
class user < activerecord::base end class member < activerecord::base end
lets suppose have 2 models called user , member , want create member whenever user created , want create user whenever member created .
if use after_create or before_create callback in both models run never ending loop .so how can done.
just check if either of association exists in db before creating in after_create
callback, this:
class user < activerecord::base after_create :create_member private def create_member unless self.member? # create member end end end class member < activerecord::base after_create :create_user private def create_user unless self.user? # create user end end end
Comments
Post a Comment