Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Interpolated association conditions and Eager loading

#6154

Suppose we have association like this:

has_one :company_relation, :foreign_key => :company_id, :primary_key => :company_id,
  :conditions => 'company_relations.owner_id = #{self.owner_id}'

In this case eager loading by this association fail because conditions are evaluated in class but not in each instance

Model.all(:include => :company_relation)



NoMethodError: undefined method `owner_id' for #<Class:0xb6635838>
    from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/association_preload.rb:381:in `interpolate_sql_for_preload'
    from /var/lib/gems/1.8/gems/will_paginate-2.3.12/lib/will_paginate/finder.rb:170:in `method_missing'
    from (eval):1:in `interpolate_sql_for_preload'
    from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/association_preload.rb:386:in `append_conditions'
    from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/association_preload.rb:366:in `find_associated_records'
    from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/association_preload.rb:230:in `preload_has_one_association'
    from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/association_preload.rb:120:in `send'
    from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/association_preload.rb:120:in `preload_one_association'
    from /var/lib/gems/1.8/gems/activesupport-2.3.8/lib/active_support/ordered_hash.rb:115:in `each'
    from /var/lib/gems/1.8/gems/activesupport-2.3.8/lib/active_support/ordered_hash.rb:115:in `each'
    from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/association_preload.rb:114:in `preload_one_association'
    from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/association_preload.rb:91:in `preload_associations'
    from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/association_preload.rb:90:in `preload_associations'
    from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/association_preload.rb:90:in `each'
    from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/association_preload.rb:90:in `preload_associations'
    from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1580:in `find_every'

Reported by Bogdan Gusiev · December 12th, 2010 @ 01:41 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Bogdan Gusiev
    Bogdan Gusiev

    Simple test to demonstrate the issue.

    December 12th, 2010 @ 01:47 PM

  2. Mick Staugaard
    Mick Staugaard

    I'm working a patch for this on rails 2.3

    January 20th, 2011 @ 01:36 AM

  3. Mick Staugaard
    Mick Staugaard

    This is a dark corner case in ActiveRecord, and it looks like this feature has been removed from ActiveRecord 3.

    Not continuing my patch.

    January 20th, 2011 @ 08:38 PM

  4. rails
    rails
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    April 21st, 2011 @ 01:00 AM

  5. rails
    rails
    • State changed from open to stale

    April 21st, 2011 @ 01:00 AM