Skip to content

Default scope breaks working of has_many :through #533

jonleighton opened this Issue May 12, 2011 · 12 comments

8 participants

Ruby on Rails member

Creating this ticket from pull request #528.

@wakeless says:

Adding a default_scope to the end of a has_many :through relationship breaks a number of things.

I've found it through a :uniq => true on the relationship, but the attached change to the tests seems to break a number of other things

@jonleighton jonleighton was assigned May 12, 2011
Fonsan commented May 12, 2011

I can confirm this bug, I get a
Mysql2::Error: Not unique table/alias: 'questions_has_sections': SELECT questions.* FROM questions INNER JOIN questions_has_sections ON questions_has_sections.question_id = INNER JOIN questions_has_sections ON = questions_has_sections.question_id WHERE questions_has_sections.section_id = 1

meaning it joins the questions_has_sections twice, I will try to create a failing test

Fonsan commented May 12, 2011

Well silly me, #528 already contained a "failing test". I tried and failed to recreate it here Fonsan@bae49f3 if it helps tracking down


The failing error I get here is,

 31) Error:
ActiveRecord::StatementInvalid: SQLite3::SQLException: near "DISTINCT": syntax error: SELECT posts.*, DISTINCT "posts".* FROM "posts" INNER JOIN "categorizations" ON "posts"."id" = "categorizations"."post_id" WHERE "categorizations"."author_id" = 2
    /Users/michaelgall/.rvm/gems/ruby-1.9.2-p136/gems/sqlite3-1.3.3/lib/sqlite3/database.rb:91:in `initialize'
    /Users/michaelgall/.rvm/gems/ruby-1.9.2-p136/gems/sqlite3-1.3.3/lib/sqlite3/database.rb:91:in `new'
    /Users/michaelgall/.rvm/gems/ruby-1.9.2-p136/gems/sqlite3-1.3.3/lib/sqlite3/database.rb:91:in `prepare'
    /Users/michaelgall/Documents/dev/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:165:in `block in exec_query'
    /Users/michaelgall/Documents/dev/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:222:in `block in log'
    /Users/michaelgall/Documents/dev/rails/activesupport/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
    /Users/michaelgall/Documents/dev/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:217:in `log'
    /Users/michaelgall/Documents/dev/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:161:in `exec_query'
    /Users/michaelgall/Documents/dev/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:372:in `select'
    /Users/michaelgall/Documents/dev/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:9:in `select_all'
    /Users/michaelgall/Documents/dev/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
    /Users/michaelgall/Documents/dev/rails/activerecord/lib/active_record/base.rb:469:in `find_by_sql'
    /Users/michaelgall/Documents/dev/rails/activerecord/lib/active_record/relation.rb:106:in `to_a'
    /Users/michaelgall/Documents/dev/rails/activerecord/lib/active_record/relation/finder_methods.rb:155:in `all'
    /Users/michaelgall/Documents/dev/rails/activerecord/lib/active_record/associations/has_many_through_association.rb:137:in `find_target'
    /Users/michaelgall/Documents/dev/rails/activerecord/lib/active_record/associations/collection_association.rb:342:in `load_target'
    /Users/michaelgall/Documents/dev/rails/activerecord/lib/active_record/associations/collection_association.rb:273:in `length'
    /Users/michaelgall/Documents/dev/rails/activerecord/lib/active_record/associations/collection_proxy.rb:47:in `length'
    test/cases/associations/has_many_through_associations_test.rb:697:in `test_joining_has_many_through_with_uniq'
    /Users/michaelgall/.rvm/gems/ruby-1.9.2-p136/gems/mocha-0.9.12/lib/mocha/integration/mini_test/version_142_to_172.rb:27:in `run'
    /Users/michaelgall/Documents/dev/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:35:in `block in run'
    /Users/michaelgall/Documents/dev/rails/activesupport/lib/active_support/callbacks.rb:408:in `_run_setup_callbacks'
    /Users/michaelgall/Documents/dev/rails/activesupport/lib/active_support/callbacks.rb:81:in `run_callbacks'
    /Users/michaelgall/Documents/dev/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:34:in `run'

The distinct is placed out of order.

This doesn't seem to be a regression from 3.0 however.

Ruby on Rails member

Thanks. I have put 3.2 as the milestone as the focus is on regressions right now.

Note to my future self: I think we should consider adding a ActiveRecord::Relation#uniq query method which would add in the DISTINCT when it builds the query, rather than adding DISTINCT as a string using select().

Fonsan commented May 13, 2011

What I am experiencing is a regression from 3.0.7

Although it does not seem to have anything to do with default_scope

Ruby on Rails member

@Fonsan: that must be a completely different issue, so I've created a separate ticket: #543

jinzhu commented Jun 29, 2011

This bug was fixed in master branch? is there anyone try to fix it for 3.0 / 3.1?

jamesw commented Jul 8, 2011

I have a similar issue in that a has_many with conditions where there is a default scope on the child class is a big issue for me in that the default scope either gets ignored buy the has
_many or can even cause issues with the has_many failing to return records when it should succeed.
I originally thought this was an RSpec bug but am assures it's a Rails issue.
Bug report to RSpec can be found here rspec/rspec-rails#408 (comment)
Not sure if I should post this as a new bug or if it is related to this issue at all


It does appear that this issue is fixed on 3.1 branch. I'm going to be seeing what it would take to back port it to Rails 3.0.x, as we are currently gem locked into Ruby 1.8.7, and cannot upgrade to Rails 3.1 and 1.9.2 immediately.

My question, what does the outlook of Rails 3.0.x look like. Would it be upgraded with new fixes and patches?

Ruby on Rails member

@pivotal-evo I think that he 3-0-stable is only to security fixes.

I'm closing this issue since it was fixed in 3-1-stable branch.

Feel free to reopen it it's still present.

kulbida commented Jul 29, 2013

This issue still remain in rails 3.2.12 with pg adapter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.