Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`foreign_type` relationship failing on Rails 6

I'm trying to understand this relationship setup on this model, and why is this an issue on Rails 6.

Coming from Rails 3, this relationship is defined as:

belongs_to :entity foreign_key: 'user_id', foreign_type: 'ruby_type'

This worked fine, all the way up to Rails 5 (At least our crawl and traceroute did not bring this as an issue).

However, once we got to Rails 6, we found the following:

ArgumentError: Unknown key: :foreign_type. Valid keys are: :class_name, :anonymous_class, :primary_key, :foreign_key, :dependent, :validate, :inverse_of, :strict_loading, :autosave, :required, :touch, :polymorphic, :counter_cache, :optional, :default

Was this syntax changed for Rails 6?

like image 786
Dynelight Avatar asked Oct 21 '25 00:10

Dynelight


1 Answers

foreign_type option can only be applied together with polymorphic - so make sure this is a polymorphic association you are after. If the association is not polymorphic I would get rid of foreign_type.

belongs_to :entity, foreign_key: 'user_id', foreign_type: 'ruby_type', polymorphic: true
like image 65
Joel Blum Avatar answered Oct 23 '25 07:10

Joel Blum



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!