On Thu, Aug 6, 2009 at 3:47 PM, Adam Grant <[email protected]> wrote:

> Thanks for the quick reply Matt,
>
> I tried :joins => [:model] instead of :include => [:model], but I get the
> same error.


I should try something like:  :joins => "LEFT JOIN jobs on jobs.person_id =
person.id")

But wait, I'm confused, why are you trying to use an include statement?
Aren't you trying to find all the ModelLogging records?
ModelLogging.count(:include => [:model])
By using an include, you are trying to load all the models related to the
ModelLogging records, but why do you need them to be loaded to count?




>
> The actual #count call does have a conditions statement in there, but I
> left it out for brevity. I've tried both ways, but they produce the same
> error. I am hesitant to use #count_by_sql, since someone might search for a
> value that is in one of the associated models, and I would have to construct
> those joins and conditions myself, which can get messy. PipingClass is not
> the only model that will be linked to ModelLoggings.


Worst case scenario, you can write a wrapper around count_by_sql.



>
>
> I've search through the restricted keywords for "model", but I didnt' find
> any reference that it could pose a problem. I dismissed it as a non-issue
> because of that lighthouse ticket someone else put up with the same problem
> I am having. I understand it is a bad practice anywho, so I will take your
> suggestion nonetheless. Do you know of an actual issue this could pose? (The
> "Rails restricted keywords" articles I've looked at are a bit dated). I'm
> curious.


I don't know for sure, I would have to look at the AR's internals. It might
be fine.


>
>
> For now, I am using the #find call, leaving out the limit, and then taking
> the size of the resulting array (UGLY). This isn't going into heavy use
> right away, so this hack will work for the time being until I get around to
> submitting that test to Rails' Lighthouse.


At least, only load the id field so the query will run faster.
I doubt AR will easily support eager loading on polymorphic associations
since you need to build a pretty complicated query. But I might be wrong.


>
>
> In case people are wondering:
>
> $ ./script/about
>
> About your application's environment
> Ruby version              1.8.6 (i686-linux)
> RubyGems version          1.3.1
> Rack version              1.0
> Rails version             2.3.2
> Active Record version     2.3.2
> Action Pack version       2.3.2
> Active Resource version   2.3.2
> Action Mailer version     2.3.2
> Active Support version    2.3.2
> Environment               development
> Database adapter          mysql
>
> Thanks!
> --
> Adam Grant
> Lead Web Engineer
> Telaeris, Inc.
> [email protected]
> (858) 627-9710
>
>
> On Thu, Aug 6, 2009 at 3:31 PM, Matt Aimonetti <[email protected]>wrote:
>
>> 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to