Samuel
Samuel

Reputation: 6136

Associations not working

When a user cancels their account on my application their user record is correctly removed from the database but their profile record still seems to exist. Below are my models for user and profile, any solutions?

Profile.rb

 class Profile < ActiveRecord::Base
    belongs_to :user
 end

user.rb

  class User < ActiveRecord::Base
   has_one :profile

   #callback that creates a profile for each user that signs up
   after_create :create_profile
   devise :database_authenticatable, :registerable,
   :recoverable, :rememberable, :trackable, :validatable

  private
    def create_profile
     self.profile = Profile.create
    end
  end

Upvotes: 0

Views: 30

Answers (2)

Victor Ronin
Victor Ronin

Reputation: 23268

You have to specify explicitly what to do with dependent model.

As example

has_one :profile, dependent: :destroy

There are some other options like :delete, :nullify. You can take a look at them here: http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_one

Upvotes: 1

Yana Agun Siswanto
Yana Agun Siswanto

Reputation: 2032

It will also delete the association too.

has_one :profile, dependent: :destroy

source: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

Upvotes: 1

Related Questions