-
Notifications
You must be signed in to change notification settings - Fork 187
Avoid ActiveRecordExtensions affects AR's belongs_to method. #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Resolved by #256 details
$ git diff
diff --git a/Gemfile b/Gemfile
index 8a435c8..d88d4fb 100644
--- a/Gemfile
+++ b/Gemfile
@@ -16,4 +16,4 @@ platforms :ruby do
gem 'sqlite3', '~> 1.4.0'
end
-gem 'activerecord', '>= 5.0.0'
+gem 'activerecord', '>= 5.0.0', '< 7.0'
$ ruby -v
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [arm64-darwin21]$ cat Gemfile.lock | grep activerecord
activerecord (6.1.6.1)
activerecord (>= 5.0.0)
activerecord-jdbcsqlite3-adapter (>= 1.3.6)$ bundle exec rspec ./spec/associations/active_record_extensions_spec.rb:138
Run options: include {:locations=>{"./spec/associations/active_record_extensions_spec.rb"=>[138]}}
.
Finished in 1.2 seconds (files took 3.8 seconds to load)
1 example, 0 failures$ bundle exec rspec spec/active_hash/base_spec.rb:1287 ./spec/associations/active_record_extensions_spec.rb:138
Run options: include {:locations=>{"./spec/active_hash/base_spec.rb"=>[1287], "./spec/associations/active_record_extensions_spec.rb"=>[138]}}
.F
Failures:
1) ActiveHash::Base active record extensions ActiveHash::Associations::ActiveRecordExtensions#belongs_to doesn't interfere with AR's procs in belongs_to methods
Failure/Error: return super unless respond_to? method_name
NoMethodError:
undefined method `current_scope' for Country:Class
# ./lib/active_hash/base.rb:259:in `method_missing'
# ./vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.6.1/lib/active_record/associations/association.rb:100:in `scope'
# ./vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.6.1/lib/active_record/associations/association.rb:218:in `find_target'
# ./vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.6.1/lib/active_record/associations/singular_association.rb:39:in `find_target'
# ./vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.6.1/lib/active_record/associations/association.rb:174:in `load_target'
# ./vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.6.1/lib/active_record/associations/association.rb:67:in `reload'
# ./vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.6.1/lib/active_record/associations/singular_association.rb:9:in `reader'
# ./vendor/bundle/ruby/3.1.0/gems/activerecord-6.1.6.1/lib/active_record/associations/builder/association.rb:103:in `country'
# ./spec/associations/active_record_extensions_spec.rb:147:in `block (4 levels) in <top (required)>'
Finished in 0.16575 seconds (files took 0.43094 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ./spec/associations/active_record_extensions_spec.rb:138 # ActiveHash::Base active record extensions ActiveHash::Associations::ActiveRecordExtensions#belongs_to doesn't interfere with AR's procs in belongs_to methods |
d71db68 to
1804ac1
Compare
|
I'm trying to think about the use case here. I've only used a where scope in those instances. In your code, what do you put into the scope (that we were dropping and you added back?) |
| if ActiveRecord::VERSION::MAJOR > 3 | ||
| it "doesn't interfere with AR's procs in belongs_to methods" do | ||
| School.belongs_to :country, lambda { where() } | ||
| School.belongs_to :country, lambda { select(:id) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this still work if someone puts a where here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've only used a where scope in those instances. In your code, what do you put into the scope (that we were dropping and you added back?)
In our use case, we have a feature that automatically decrypts the contents when a column is loaded. That process can take a long time, and we are optimizing performance by skipping that process by not loading unnecessary columns. We use select to achieve this.
does this still work if someone puts a where here?
Sure. I searched and thought it would be more common for use cases to use "where" in scope, so I modified the test to use "where" as well. What do you think? 20cb9bc
This is the search results.
https://github.com/search?l=Ruby&q=belongs_to%3A+lambda&type=Code
|
Thanks @machisuke The where clauses on the scope makes a ton of sense. Wise to put that into the test case. |
kbrock
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is minor and the tests suggest that it does not have many (any?) side effects.
Anyone else have an opinion?
|
Can you rebase the 256 PR code out of here? |
|
@machisuke thanks If you could rebase this on master (to remove #256) that would be great. |
|
@kbrock |
|
@machisuke thanks I merge another and introduced a conflict for this. sorry. Please let me know if I introduced a mistake while resolving this merge or introduced something that you did not want. |
Background
ActiveRecord#belongs_to accepts these args. ref
But , ActiveRecordExtensions drops
scopearguments.active_hash/lib/associations/associations.rb
Lines 6 to 21 in b11cb2e
Changes
In this PR, by calling super with no arguments, all arguments including scope are passed to super.