has_many.count does not seem to support an options hash like ActiveRecord::Base.count does. Example: >> AcademicProgram.find(:first).course_requirements.count :conditions => "courses.name LIKE ''a%''", :include => :course => 40 >> ap.ap_requirements.count :conditions => "courses.name LIKE ''a%''", :include => :course ActiveRecord::StatementInvalid: Mysql::Error: #42000You have an error in your SQ L syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''includecourse))'' at line 1: SELECT count(*) AS count_ all FROM ap_requirements WHERE (ap_requirements.academic_program_id = 1 AND (con ditionscourses.name LIKE ''a%''includecourse)) from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor d/connection_adapters/abstract_adapter.rb:120:in `log'' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor d/connection_adapters/mysql_adapter.rb:185:in `execute'' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor d/connection_adapters/mysql_adapter.rb:337:in `select'' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor d/connection_adapters/mysql_adapter.rb:180:in `select_one'' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor d/connection_adapters/abstract/database_statements.rb:16:in `select_value'' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor d/calculations.rb:161:in `execute_simple_calculation'' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor d/calculations.rb:141:in `calculate'' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor d/calculations.rb:64:in `count'' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor d/associations/has_many_association.rb:47:in `count'' from (irb):21 It appears to call to_s on whatever is passed to it. Is this a bug, normal behavior, or am I doing something wrong? Thanks. -- Jack Christensen jackc@hylesanderson.edu
I suppose I should check edge rails a little more often. This was fixed on 4/19/06 in Changeset 4237. Thanks. Jack Christensen wrote:> has_many.count does not seem to support an options hash like > ActiveRecord::Base.count does. > > Example: > >> AcademicProgram.find(:first).course_requirements.count :conditions > => "courses.name LIKE ''a%''", :include => :course > => 40 > >> ap.ap_requirements.count :conditions => "courses.name LIKE ''a%''", > :include => :course > ActiveRecord::StatementInvalid: Mysql::Error: #42000You have an error > in your SQ > L syntax; check the manual that corresponds to your MySQL server > version for the > right syntax to use near ''includecourse))'' at line 1: SELECT count(*) > AS count_ > all FROM ap_requirements WHERE (ap_requirements.academic_program_id = > 1 AND (con > ditionscourses.name LIKE ''a%''includecourse)) > from > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor > d/connection_adapters/abstract_adapter.rb:120:in `log'' > from > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor > d/connection_adapters/mysql_adapter.rb:185:in `execute'' > from > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor > d/connection_adapters/mysql_adapter.rb:337:in `select'' > from > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor > d/connection_adapters/mysql_adapter.rb:180:in `select_one'' > from > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor > d/connection_adapters/abstract/database_statements.rb:16:in > `select_value'' > from > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor > d/calculations.rb:161:in `execute_simple_calculation'' > from > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor > d/calculations.rb:141:in `calculate'' > from > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor > d/calculations.rb:64:in `count'' > from > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_recor > d/associations/has_many_association.rb:47:in `count'' > from (irb):21 > > It appears to call to_s on whatever is passed to it. > > Is this a bug, normal behavior, or am I doing something wrong? > > Thanks. >-- Jack Christensen jackc@hylesanderson.edu
Apparently Analagous Threads
- odd error with sqlite3 adaptor
- a possible bug in Has_many :through using :soucre
- Record retrieval in Many-to-many using :through not working
- undefined method `fullname' for #<User:0x357e380>, BUT works on first view?
- Problems connecting to remote Oracle DB on windows using active record