[Rails5] Fix `Route source can't be blank`

In Rails 5.0 automatic inverse does not work for polymorphic relathionships.
It was fixed in Rails 5.2: https://github.com/rails/rails/pull/28808

Until that the `inverse_of: :source` argument should be set explicitly.
This commit is contained in:
blackst0ne 2018-05-11 11:19:59 +11:00
parent 35816eb7be
commit c3e40ed8ff
1 changed files with 3 additions and 1 deletions

View File

@ -4,7 +4,9 @@ module Routable
extend ActiveSupport::Concern
included do
has_one :route, as: :source, autosave: true, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
# Remove `inverse_of: source` when upgraded to rails 5.2
# See https://github.com/rails/rails/pull/28808
has_one :route, as: :source, autosave: true, dependent: :destroy, inverse_of: :source # rubocop:disable Cop/ActiveRecordDependent
has_many :redirect_routes, as: :source, autosave: true, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
validates :route, presence: true