samedi 12 janvier 2019

how delete user and posts of user

i have two model User and post. user have has_many posts and post are based on user. I want When user is delete automatically The posts of that user should be delete how to do this i am very new to ruby..

class User < ApplicationRecord
    has_many :posts
end

class User < ApplicationRecord
    belongs_to :user
end

class UsersController < ApplicationController
    def destroy
    @user = User.find_by(id:params[:id])
    @user.posts
    @user.destroy
end

I am try the above code but only user are deleted His posts are not deleted Thanks Advance!

Aucun commentaire:

Enregistrer un commentaire