Comments (2)
One of the differences between the first and second assertions is that the second assertion chains the parents
method to the Category.where(id: category.id)
query. In other words, it calls the parents
method on the result of the Category.where(id: category.id)
query. Since the parent
record is not included in the result of the Category.where(id: category.id)
query, calling the parents
method on this result will not return the parent record.
To prove this, you can call Category.parents.pluck(:id)
, which should pass the test. In this case, all Category
records will be available for the parent
chain method.
assert_equal([parent.id], Category.parents.pluck(:id))
Trivial: Instead of using a class method, you can use a scope for the same purpose.
from rails.
Hi @danielwaterworth , thanks for the report. The two queries are not the same. If you were to expand the self.parents
method on the base scope in the second query, it would look something like this:
Category.where(id: category.id).parents.pluck(:id) #expands to
Category.where(id: category.id).where(id: Category.where(id: category.id).select(:parent_category_id)).pluck(:id))
Your base base scope still will have .where(id: category.id)
on it.
The first one is this:
Category.where(id: Category.where(id: category.id).select(:parent_category_id)).pluck(:id))
hence the difference.
Let me know if you still some issue with this. Thanks!
from rails.
Related Issues (20)
- Rails 7.1 changing behaviour of rename_table in existing migrations HOT 8
- `ActiveRecord::ConnectionAdapters::SchemaStatements#add_index` does not document new option `nulls_not_distinct` HOT 3
- Rails Guides redesign overlapping on wide display.
- reject_if callback evaluation point changed in rails 7.2 HOT 2
- Calling connection.active? after establish_connection returns false since AR 7.2.0 HOT 5
- `destroyed_by_association` is not being set on belongs_to association HOT 2
- [Bug] [ActiveSupport] "rails destroy model Adress" [sic] does not check first if the model exists
- TRUSTED_PROXIES behavior inconsistency due to IPv4-mapped address handling change in Ruby 3.1+ HOT 2
- [ActiveRecord::Encryption] Allow deterministic encryption keys to be rotated HOT 1
- [ActiveRecord::Encryption] Is it possible to store the data in a JSONB column? HOT 2
- ActiveJob run multiple jobs per single enqueue in Rails 7.1 HOT 2
- Cannot run `bin/rubocop --auto-gen-config` with default binstub HOT 3
- `ActionController::RoutingError` raised when using middleware with `recognize_path` HOT 2
- Bug with pluck method when used "where" condition with raw SQL HOT 3
- Can't rollback if migration contains `add_reference` when foreign_key.to_table uses schema path HOT 2
- Syntax error in user.rb when using lambdas
- CI fails with the code generated by Rails version >= 7.2.0 HOT 1
- Changed behaviour if CurrentAttributes caused by new default functionality HOT 3
- Support the Prism parser with ErrorHighlight HOT 1
- `ActionController::Live` and `ActiveRecord::Base.connected_to` stale db state HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rails.