aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/has_many_through_association.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/associations/has_many_through_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb24
1 files changed, 19 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb
index 3d3d53c677..573e29ca31 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -40,6 +40,21 @@ module ActiveRecord
end
protected
+ def through_reflection
+ unless @through_reflection ||= @owner.class.reflections[@reflection.options[:through]]
+ raise ActiveRecordError, "Could not find the association '#{@reflection.options[:through]}' in model #{@reflection.klass}"
+ end
+ @through_reflection
+ end
+
+ def source_reflection
+ @source_reflection_name ||= @reflection.name.to_s.singularize.to_sym
+ unless @source_reflection ||= through_reflection.klass.reflect_on_association(@source_reflection_name)
+ raise ActiveRecordError, "Could not find the source association '#{@source_reflection_name}' in model #{@through_reflection.klass}"
+ end
+ @source_reflection
+ end
+
def method_missing(method, *args, &block)
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
super
@@ -61,12 +76,8 @@ module ActiveRecord
end
def construct_conditions
- unless through_reflection = @owner.class.reflections[@reflection.options[:through]]
- raise ActiveRecordError, "Could not find the association '#{@reflection.options[:through]}' in model #{@reflection.klass}"
- end
-
# Get the actual primary key of the belongs_to association that the reflection is going through
- source_primary_key = through_reflection.klass.reflect_on_association(@reflection.name.to_s.singularize.to_sym).primary_key_name
+ source_primary_key = source_reflection.primary_key_name
if through_reflection.options[:as]
conditions =
@@ -119,6 +130,9 @@ module ActiveRecord
end
end
+ def sql_conditions
+ @conditions ||= interpolate_sql(@reflection.active_record.send(:sanitize_sql, through_reflection.options[:conditions])) if through_reflection.options[:conditions]
+ end
end
end
end