Hmm "belongs_to :model, :polymorphic => true" <-- using model has a class/model name isn't recommended, as a work around I would use #count passing a condition and/or a join or even directly use count_by_sql.
- Matt On Thu, Aug 6, 2009 at 3:24 PM, Adam Grant <[email protected]> wrote: > Hi group! > > My question is this: Is there a way to turn of eager loading for a #count > call on a model (ModelLogging.eager_loading = false ??), since that SEEMS to > be my problem (#count doesn't seem to take the :preload option). > > The issue I have is with calling count on a Polymorphic model: > > class ModelLogging < ActiveRecord::Base > belongs_to :model, :polymorphic => true > end > > class PipingClass < ActiveRecord::Base > has_many :model_loggings, :as => :model > end > > The model_loggings table looks like: > =============== > create_table "model_loggings", :force => true do |t| > t.string "model_type" > t.integer "model_id" > t.string "field_name" > t.string "old_value" > t.string "new_value" > t.datetime "modified_at" > t.integer "employee_id" > t.datetime "created_at" > t.datetime "updated_at" > end > ============== > > There is only one record in the model_loggings table: > =============== > #<ModelLogging id: 1, model_type: "PipingClass", model_id: 3331, > field_name: "class_code", old_value: "BCA", new_value: "A", modified_at: > "2009-08-06 11:55:44", employee_id: nil, created_at: "2009-08-06 11:55:44", > updated_at: "2009-08-06 11:55:44"> > =============== > > If I try the following, it works: > =============== > > ModelLogging.find(:all, :include => [:model]) > => [#<ModelLogging id: 1, model_type: "PipingClass", model_id: 3331, > field_name: "class_code", old_value: "BCA", new_value: "A", modified_at: > "2009-08-06 11:55:44", employee_id: nil, created_at: "2009-08-06 11:55:44", > updated_at: "2009-08-06 11:55:44">] > > > ModelLogging.count > => 1 > =============== > > But... > =============== > > ModelLogging.count(:include => [:model]) > => ActiveRecord::EagerLoadPolymorphicError: Can not eagerly load the > polymorphic association :model > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1960:in > `initialize' > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1853:in > `new' > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1853:in > `build_join_association' > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1836:in > `build' > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1839:in > `build' > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1838:in > `each' > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1838:in > `build' > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:1758:in > `initialize' > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/calculations.rb:210:in > `new' > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/calculations.rb:210:in > `construct_calculation_sql' > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/calculations.rb:243:in > `execute_simple_calculation' > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/calculations.rb:134:in > `calculate' > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/calculations.rb:130:in > `catch' > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/calculations.rb:130:in > `calculate' > from > /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/calculations.rb:48:in > `count_without_trace_ActiveRecord___self_name__find' > from > /home/telaeris/sandbox/po_tracker_bpcherrypoint/vendor/plugins/newrelic_rpm/lib/new_relic/agent/method_tracer.rb:138:in > `count_without_trace_ActiveRecord_find' > ... 55 levels... > from > /usr/local/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/handler/mongrel.rb:61:in > `process' > from > /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in > `process_client' > from > /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `each' > from > /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in > `process_client' > from > /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run' > from > /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in > `initialize' > from > /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `new' > from > /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run' > from > /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in > `initialize' > from > /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `new' > from > /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `run' > from > /usr/local/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/handler/mongrel.rb:34:in > `run' > from > /home/telaeris/.gem/ruby/1.8/gems/rails-2.3.2/lib/commands/server.rb:111 > from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in > `gem_original_require' > from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in > `require' > ================ > > Not sure what to do without diving into AR internals some more, something I > don't really want to do. > > I've also found this ticket, but no action has really happened on it. I am > pondering submitting a failing test case for it if I can't think of another > way around this: > > > https://rails.lighthouseapp.com/projects/8994/tickets/1166-count-works-differently-than-find-with-polymorphic-include > > Any thoughts? > > PS: I should be at tonight's meeting, and I am pushing to attend the RBBQ, > but I may be fishing for longer than expected that day. > > Thanks! > > -- > Adam Grant > Lead Web Engineer > Telaeris, Inc. > [email protected] > (858) 627-9710 > > > > --~--~---------~--~----~------------~-------~--~----~ SD Ruby mailing list [email protected] http://groups.google.com/group/sdruby -~----------~----~----~----~------~----~------~--~---
