#3 new
Jon Evans

error with Rails 2.3.2 & nested scoped association counts

Reported by Jon Evans | March 25th, 2009 @ 12:22 PM

I built a test app with these models:


class Person < ActiveRecord::Base
  has_many :houses
  
  has_many :tennants, :through => :houses
  has_many :friends, :through => :tennants
end

class House < ActiveRecord::Base
  has_many :tennants
end

class Tennant < ActiveRecord::Base
  has_many :friends
end

class Friend < ActiveRecord::Base
  named_scope :bob, :conditions=>"name like '%bob%'"
end

From the console, I can create a chain of associated person, house, tennant and friend objects. It all works until I try to access the count of a named scope on the nested association. This worked OK in Rails 2.3.1 but is broken in Rails 2.3.2.


Loading development environment (Rails 2.3.2)
test> p = Person.f #=> #<Person id: 1, name: nil>
  Person Load (0.5ms)   SELECT * FROM "people" LIMIT 1

test> p.houses #=> [#<House id: 1, name: nil, person_id: 1>]
  House Load (0.3ms)   SELECT * FROM "houses" WHERE ("houses".person_id = 1)

test> p.tennants #=> [#<Tennant id: 1, house_id: 1, name: nil>]
  Tennant Load (0.3ms)   SELECT "tennants".* FROM "tennants" INNER JOIN houses ON ( tennants.house_id = houses.id ) WHERE (houses.person_id = 1 )

test> p.friends #=> [#<Friend id: 1, tennant_id: 1, name: "Bob">]
  Friend Load (0.4ms)   SELECT "friends".* FROM "friends" INNER JOIN tennants ON ( friends.tennant_id = tennants.id ) INNER JOIN houses ON ( tennants.house_id = houses.id ) WHERE (houses.person_id = 1 )

test> p.friends.bob #=> [#<Friend id: 1, tennant_id: 1, name: "Bob">]
  Friend Load (0.3ms)   SELECT "friends".* FROM "friends" INNER JOIN tennants ON ( friends.tennant_id = tennants.id ) INNER JOIN houses ON ( tennants.house_id = houses.id ) WHERE (houses.person_id = 1 ) AND ((friends.name like '%bob%') AND (houses.person_id = 1 ))

test> p.friends.bob.count
  SQL (0.0ms)   SQLite3::SQLException: near "*": syntax error: SELECT count("friends".*) AS count_friends_all FROM "friends" INNER JOIN tennants ON ( friends.tennant_id = tennants.id ) INNER JOIN houses ON ( tennants.house_id = houses.id ) WHERE (((friends.name like '%bob%') AND (houses.person_id = 1 )) AND (houses.person_id = 1 )) 
ActiveRecord::StatementInvalid: SQLite3::SQLException: near "*": syntax error: SELECT count("friends".*) AS count_friends_all FROM "friends" INNER JOIN tennants ON ( friends.tennant_id = tennants.id )  INNER JOIN houses ON ( tennants.house_id = houses.id ) WHERE (((friends.name like '%bob%') AND (houses.person_id = 1 )) AND (houses.person_id = 1 )) 
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract_adapter.rb:212:in `log'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/sqlite_adapter.rb:157:in `execute'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/sqlite_adapter.rb:402:in `catch_schema_changes'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/sqlite_adapter.rb:157:in `execute'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/sqlite_adapter.rb:305:in `select'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:13:in `select_one'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:19:in `select_value'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/calculations.rb:243:in `execute_simple_calculation'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/calculations.rb:134:in `calculate'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/calculations.rb:130:in `catch'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/calculations.rb:130:in `calculate'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/calculations.rb:48:in `count'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:176:in `count'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2148:in `with_scope'
... 2 levels...
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_proxy.rb:145:in `send'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_proxy.rb:145:in `send'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/named_scope.rb:183:in `method_missing'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2148:in `with_scope'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_proxy.rb:206:in `send'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_proxy.rb:206:in `with_scope'
	from (__DELEGATION__):2:in `__send__'
	from (__DELEGATION__):2:in `with_scope'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/named_scope.rb:182:in `method_missing'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2148:in `with_scope'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_proxy.rb:206:in `send'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_proxy.rb:206:in `with_scope'
	from (__DELEGATION__):2:in `__send__'
	from (__DELEGATION__):2:in `with_scope'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/named_scope.rb:179:in `method_missing'
	from (irb):6test> 

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

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

People watching this ticket

Pages