This project is archived and is in readonly mode.

#4887 ✓resolved
Bogdan Gusiev

has_many through belongs_to association bug

Reported by Bogdan Gusiev | June 17th, 2010 @ 11:36 AM | in 2.3.10

I am finding the bug in has_many through belongs_to association:

class Post < AR::Base
  belongs_to :author
  has_many :favorites, :through => :author
end

Let's say that our app allows anonymous posts. In this case author_favorites association should return empty array for anonymous posts:

post = Post.create!(:title => 'title', :body => 'body')
assert_equal post.favorites.size, 0

Second line throws an exception:

ActiveRecord::StatementInvalid: PGError: ERROR:  syntax error at or near ")"
LINE 1: ...ites".author_id = "authors".id WHERE     (("authors".id = ))
                                                                     ^
: SELECT     COUNT(*) AS count_id FROM       "author_favorites" INNER JOIN "authors" ON "author_favorites".author_id = "authors".id WHERE     (("authors".id = ))
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:210:in `log'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:464:in `execute_without_query_record'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/test/cases/helper.rb:40:in `execute'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:954:in `select_raw'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:941:in `select'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:56:in `select_all'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:13:in `select_one'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:19:in `select_value'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/relation/calculations.rb:174:in `execute_simple_calculation'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/relation/calculations.rb:161:in `perform_calculation'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/relation/calculations.rb:128:in `calculate'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/relation/calculations.rb:45:in `count'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/base.rb:406:in `__send__'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/base.rb:406:in `count'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/associations/association_collection.rb:195:in `count'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/base.rb:1167:in `with_scope'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/associations/association_collection.rb:195:in `send'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/associations/association_collection.rb:195:in `count'
    /home/bogdan/makabu/opensource/ruby/rails/activerecord/lib/active_record/associations/has_many_through_association.rb:32:in `size'
    ./test/cases/associations/has_many_through_associations_test.rb:368:in `test_has_many_association_through_a_belongs_to_association_if_belongs_to_is_nil'
    /home/bogdan/makabu/opensource/ruby/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:67:in `__send__'
    /home/bogdan/makabu/opensource/ruby/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:67:in `run'
    /home/bogdan/makabu/opensource/ruby/rails/activesupport/lib/active_support/callbacks.rb:417:in `_run_setup_callbacks'
    /home/bogdan/makabu/opensource/ruby/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:65:in `run'

It tries to build a join through unexisted relation.

Thanks,
Bogdan.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages