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.

Through Associations with Conditions Fails

#2361

Suppose you have a class as such:


class Earning
  belongs_to :earning_category,
    :conditions => 'department_code = \'#{department_code_for_instance}\''
end

And another like this:


class Employee
  has_many :earnings, :include => :earning_category
end

You will have an error like the following when attempting to access the 'has_many :earnings' association on an Employee instance:


NoMethodError: undefined method `department_code' for #<ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation:0x685c5cc>
        from /vendor/rails/activerecord/lib/active_record/associations.rb:2143:in `interpolate_sql'
        from /vendor/rails/activerecord/lib/active_record/associations.rb:2104:in `association_join'
        from /vendor/rails/activerecord/lib/active_record/associations.rb:2103:in `each'
        from /vendor/rails/activerecord/lib/active_record/associations.rb:2103:in `association_join'
        from /vendor/rails/activerecord/lib/active_record/associations.rb:1566:in `construct_finder_sql_with_included_associations'
        from /vendor/rails/activerecord/lib/active_record/associations.rb:1566:in `collect'
        from /vendor/rails/activerecord/lib/active_record/associations.rb:1566:in `construct_finder_sql_with_included_associations'
        from /vendor/rails/activerecord/lib/active_record/associations.rb:1558:in `select_all_rows'
        from /vendor/rails/activerecord/lib/active_record/associations.rb:1335:in `find_with_associations'
from C:/projects/409a/vendor/rails/activerecord/lib/active_record/base.rb:1551:in
`find_every'
        from /vendor/rails/activerecord/lib/active_record/base.rb:615:in `find'
        from /vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:60:in `find'
        from /vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:395:in `find_target'
        from /vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:349:in `load_target'
        from /vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:139:in `inspect'

It seems to me that the interpolation of the belongs_to conditions is happening in the wrong context.

Reported by James Herdman · March 27th, 2009 @ 03:36 PM

State: invalid
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from 2.3.2, :include, belongs_to, has_many to 2.3.2, :include, belongs_to, bugmash, has_many

    August 5th, 2009 @ 03:51 PM

  2. dira
    dira

    not reproducible

    What I understand is:
    - Employee has field department_code_for_instance - EarningCategory has field department_code

    • my_employee.earnings is intended to apply the condition on each earning_category in the context of my_employee which does not make sense (an Earning object could not be created on its own).

    The test fails - legitimately - on evaluating department_code_for_instance (not on department_code).

    August 9th, 2009 @ 11:38 AM

  3. Rizwan Reza
    Rizwan Reza
    • State changed from new to invalid
    • Tag changed from 2.3.2, :include, belongs_to, bugmash, has_many to 2.3.2, :include, belongs_to, has_many

    January 20th, 2010 @ 11:03 AM