ruby - Delete all in rails console -


i have association user user has_many agents , agent belongs_to user. in rails console,i trying use different users test particular scenario , want user no agents,hence want delete user.agents. tried user.agents.map(&:destroy),but gives error activerecord::staleobjecterror: attempted delete stale object.i tried user.agents.delete_all not work.can delete users agents single command in rails console.

you better use destroy because goes through rails magic (callbacks , such)

user.destroy #for single record user.agents.destroy_all #for collection 

Comments