allow passing false to :polymorphic option of belongs_to#40879
Merged
kamipo merged 1 commit intorails:masterfrom Dec 18, 2020
Merged
allow passing false to :polymorphic option of belongs_to#40879kamipo merged 1 commit intorails:masterfrom
kamipo merged 1 commit intorails:masterfrom
Conversation
Contributor
Author
|
here's a single-file case with awesome_nested_set. # frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "activerecord", "6.1.0"
gem "sqlite3"
gem "awesome_nested_set", require: false
end
require "active_record"
require "awesome_nested_set"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :categories do |t|
t.string :name
t.integer :parent_id, null: true, index: true
t.integer :lft, null: false, index: true
t.integer :rgt, null: false, index: true
end
end
class Category < ActiveRecord::Base
acts_as_nested_set
end |
kamipo
reviewed
Dec 18, 2020
Comment on lines
1359
to
1361
Member
There was a problem hiding this comment.
belongs_to :category on Category model is a bit weird.
Can you change the name to Post for example?
kamipo
reviewed
Dec 18, 2020
Member
There was a problem hiding this comment.
Can you move :polymorphic to the above line instead? It is always valid option regardless of the value.
diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb
index 869b228dc7..584af2c3f2 100644
--- a/activerecord/lib/active_record/associations/builder/belongs_to.rb
+++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -7,8 +7,8 @@ def self.macro
end
def self.valid_options(options)
- valid = super + [:counter_cache, :optional, :default]
- valid += [:polymorphic, :foreign_type] if options[:polymorphic]
+ valid = super + [:polymorphic, :counter_cache, :optional, :default]
+ valid += [:foreign_type] if options[:polymorphic]
valid += [:ensuring_owner_was] if options[:dependent] == :destroy_async
valid
end10368e2 to
e9adaea
Compare
before this, passing false would raise the following error because a condition in AR would disregard the option entirely if false was passed. ArgumentError: Unknown key: :polymorphic. Valid keys are: :class_name, :anonymous_class, :primary_key, :foreign_key, :dependent, :validate, :inverse_of, :strict_loading, :autosave, :required, :touch, :counter_cache, :optional, :default
e9adaea to
5b2332a
Compare
Member
|
Thanks! |
kamipo
added a commit
that referenced
this pull request
Dec 18, 2020
allow passing false to :polymorphic option of belongs_to
Contributor
Author
|
for anybody coming across this and being in a hurry, i'm using this monkey patch: # config/initializers/active_record.rb
module PolymorphicBelongsTo
def valid_options(options)
valid = super + [:polymorphic, :counter_cache, :optional, :default]
valid += [:foreign_type] if options[:polymorphic]
valid += [:ensuring_owner_was] if options[:dependent] == :destroy_async
valid
end
end
ActiveSupport.on_load :active_record do
ActiveRecord::Associations::Builder::BelongsTo.extend PolymorphicBelongsTo
end |
anitagraham
added a commit
to anitagraham/refinerycms-inquiries
that referenced
this pull request
Jan 1, 2021
Add puma and selenium-webdriver to Gemfile. Upgrade refinery version to 4.1.0 in gemspec Leave Rails at 6.0.0 until 6.1 problem with polymorphic is released (rails/rails#40879, includes a patch for ActiveRecord in the comments) Add rubocop fix for factory values Minor changes to allow for matching first of several page elements and how to click on them.
anitagraham
added a commit
to anitagraham/refinerycms-inquiries
that referenced
this pull request
Jan 22, 2021
Changed recieved to received Add puma and selenium-webdriver to Gemfile. Upgrade refinery version to 4.1.0 in gemspec Leave Rails at 6.0.0 until 6.1 problem with polymorphic is released (rails/rails#40879, includes a patch for ActiveRecord in the comments) Add rubocop fix for factory values Spec changes to allow for matching first of several page elements and how to click on them. Work around a problem with polymorphism at Rails 6.0.3 Use Rails credentials to store and retrieve recaptcha keys Include Refinery::Testing::ControllerMacros::Authentication through spec_helper, All tests pass
parndt
added a commit
to refinery/refinerycms-inquiries
that referenced
this pull request
Jan 17, 2025
* Changed recieved to received Add puma and selenium-webdriver to Gemfile. Upgrade refinery version to 4.1.0 in gemspec Leave Rails at 6.0.0 until 6.1 problem with polymorphic is released (rails/rails#40879, includes a patch for ActiveRecord in the comments) Add rubocop fix for factory values Minor changes to allow for matching first of several page elements and how to click on them. * Try to get more specific error from Inquiries * Work around a problem with polymorphism at Rails 6.0.3 * Use Rails credentials for recaptcha keys * Add configuration for privacy_link Add test gems/drivers as used for refinerycms Testing: moved 'making_an_inquiry' into spec/support/spec_helper updated spec/spec_helper to match refinerycms * Remove Rails 6.0.3 workaround * Update rails dependency to ~>6.1 --------- Co-authored-by: Philip Arndt <git@p.arndt.io>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AR 6.1 will raise the following error because a condition would disregard the option
:polymorphicentirely if false was passed.Other Information
i stumbled upon this using the awesome_nested_set gem.
it has options to configure polymorphism and always passes this option (with the default of
false).possibly related to #40830.
i added a test case. it's kind of weird though. please advise.