Search code examples
ruby-on-railsactiverecorddependent-destroy

rails dependent destroy looking for non existing table


I am getting error:

PG::UndefinedTable: ERROR: relation "profiles_rsl_codes" does not exist LINE 5:

when I try to destroy a profile.

I have a table called rsl_codes_profiles and in my profile model I have

  has_many :rsl_codes_profiles, class_name: "RslCodesProfile", dependent: :destroy

and in my RslCodesProfile class I have:

class RslCodesProfile < ActiveRecord::Base
  belongs_to :rsl_code
  belongs_to :profile

  validates :rsl_code_id, :presence => true
  validates :profile_id, :presence => true
  validates :rel_type, :presence => true
end

there may have been some migrating and undoing of migration and changing name of that table and then remigrating in case that might have had an influence.

A global search of my application does not find any reference to profiles_rsl_codes or ProfilesRslCodes or the singular of those.

The error backtrace only points to where I do @profile.destroy and the rest of the trace is all framework stuff.

Any ideas?


Solution

  • Apologies to those who spent time trying to help me on this one. The mistake was in code I did not add to the question.

    I had to change this in my RslCode model

    has_and_belongs_to_many :visible_rsl_codes, class_name: "RslCode", foreign_key: "code_visible_to_profile_ids"
    has_and_belongs_to_many :visible_comment_rsl_codes, class_name: "RslCode", foreign_key: "comment_visible_to_profile_ids"
    

    which was trying to use a field in RslCode that I had since removed code_visible_to_profile_ids and comment_visible_to_profile_ids

    to this

    has_many :code_rsl_code_profiles, -> { where rel_type: 'code' }, class_name: 'RslCodesProfile', source: :rsl_codes_profiles
    has_many :visible_rsl_codes, :through => :code_rsl_code_profiles, class_name: 'RslCode', source: :rsl_code
    
    has_many :comment_rsl_code_profiles, -> { where rel_type: 'comment' }, class_name: 'RslCodesProfile', source: :rsl_codes_profiles
    has_many :visible_comment_rsl_codes, :through => :comment_rsl_code_profiles, class_name: 'RslCode', source: :rsl_code
    

    as I added joins tables to achieve the result and did not adjust those relationships.