This project is archived and is in readonly mode.
Through Associations with Conditions Fails
Reported by James Herdman | March 27th, 2009 @ 03:36 PM | in 2.x
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.
Comments and changes to this ticket
-
CancelProfileIsBroken August 5th, 2009 @ 03:51 PM
- Tag changed from 2.3.2, :include, belongs_to, has_many to 2.3.2, :include, belongs_to, bugmash, has_many
-
dira August 9th, 2009 @ 11:38 AM
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).
-
Rizwan Reza January 20th, 2010 @ 11:03 AM
- 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
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>