aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2006-03-18 23:14:31 +0000
committerRick Olson <technoweenie@gmail.com>2006-03-18 23:14:31 +0000
commit9c9069a67595f620f80eabc475181cb36a26cdde (patch)
treea2a8aebf762be6d73ec8cab1d5f0a1c8eb84356b /activerecord/lib
parenta46214c4f9e7117796f2a5ed1414a83c2ca4c6bd (diff)
downloadrails-9c9069a67595f620f80eabc475181cb36a26cdde.tar.gz
rails-9c9069a67595f620f80eabc475181cb36a26cdde.tar.bz2
rails-9c9069a67595f620f80eabc475181cb36a26cdde.zip
Fixed has_many :through to include :conditions set on the :through association. closes #4020 [jonathan@bluewire.net.nz]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3958 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb10
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb24
2 files changed, 26 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 4218174442..ede8fd1223 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1157,7 +1157,7 @@ module ActiveRecord
class JoinBase
attr_reader :active_record
- delegate :table_name, :column_names, :primary_key, :reflections, :to => :active_record
+ delegate :table_name, :column_names, :primary_key, :reflections, :sanitize_sql, :to => :active_record
def initialize(active_record)
@active_record = active_record
@@ -1244,7 +1244,7 @@ module ActiveRecord
case
when reflection.macro == :has_many && reflection.options[:through]
through_reflection = parent.active_record.reflect_on_association(reflection.options[:through])
- through_conditions = through_reflection.options[:conditions] ? "AND #{eval("%(#{through_reflection.active_record.send :sanitize_sql, through_reflection.options[:conditions]})")}" : ''
+ through_conditions = through_reflection.options[:conditions] ? "AND #{interpolate_sql(sanitize_sql(through_reflection.options[:conditions]))}" : ''
if through_reflection.options[:as] # has_many :through against a polymorphic join
polymorphic_foreign_key = through_reflection.options[:as].to_s + '_id'
polymorphic_foreign_type = through_reflection.options[:as].to_s + '_type'
@@ -1296,7 +1296,7 @@ module ActiveRecord
aliased_table_name,
reflection.active_record.connection.quote_column_name(reflection.active_record.inheritance_column),
klass.quote(klass.name)] if sti?
- join << "AND #{eval("%(#{reflection.active_record.send :sanitize_sql, reflection.options[:conditions]})")} " if reflection.options[:conditions]
+ join << "AND #{interpolate_sql(sanitize_sql(reflection.options[:conditions]))} " if reflection.options[:conditions]
join
end
@@ -1316,6 +1316,10 @@ module ActiveRecord
def table_name_and_alias
table_alias_for table_name, @aliased_table_name
end
+
+ def interpolate_sql(sql)
+ instance_eval("%@#{sql.gsub('@', '\@')}@")
+ end
end
end
end
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