Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

has_many dependent: destroy is searching for a wrong column name

I have a class :klass that has_many :otherklass, and it's dependent: :destroy.

However, when I try to destroy a :klass, rails is looking for otherklass.namespace_klass_id = klass.id, and understandably not finding anything.

Is there a way to tell rails explicitly that :otherklass uses .namespace_klass_id as the column on otherklass?

like image 618
Mike Manfrin Avatar asked Oct 24 '25 08:10

Mike Manfrin


1 Answers

After some more searching, a coworker and I found that the answer is adding:

foreign_key: :namespace_klass_id

So it looks like:

has_many :otherklass, foreign_key: :namespace_klass_id, dependent: :destroy
like image 73
Mike Manfrin Avatar answered Oct 27 '25 04:10

Mike Manfrin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!