For example
class User < ActiveRecord::Base
has_one :avatar, :dependent => :destroy
end
class Avatar < ActiveRecord::Base
belongs_to :user
end
What if I have a User with an Avatar and then do something like
old_avatar = user.avatar
new_avatar = Avatar.new
user.avatar = new_avatar
user.save
old_avatar.destroy
So I don’t want do the last command manually (destroy). Is there any options for has_many to autodestroy old object? Or many it’s better use something else instead of avatar= method?
Thank you.
Why not update old avatar instead of creating new one like:
Also, you might create
after_savecallback insideAvatarmodel if you like.