In my web application users have the ability to upload games which are then associated to each user by a user_id column in the games table.
What I want to do now is whenever a user deletes their profile then the games associated with them are also erased.
I am wondering how I would go about doing this?
In the destroy method the users controller I currently have:
def destroy
@user = User.find_by_username(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :ok }
end
end
Would it be some sort of @user.id == @game.user_id.destroy?
Add :dependent => :destroy when you declare the association in the model:
has_many :games, :dependent => :destroy