diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-03-05 20:10:24 +0000 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-03-05 20:10:24 +0000 |
commit | ddf83d14f1c7ddae07a285a8ad7c45f652edc843 (patch) | |
tree | f46cb89576a6267e6a501acec783ee12c1805a89 /activerecord/lib/active_record | |
parent | 4206eff1895dccadcbec471798bfbd129404cc94 (diff) | |
download | rails-ddf83d14f1c7ddae07a285a8ad7c45f652edc843.tar.gz rails-ddf83d14f1c7ddae07a285a8ad7c45f652edc843.tar.bz2 rails-ddf83d14f1c7ddae07a285a8ad7c45f652edc843.zip |
Add a test for STI on the through where the through is nested, and change the code which support this
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/associations/through_association.rb | 35 | ||||
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 3 |
2 files changed, 25 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb index ed24373cba..8e9259a28d 100644 --- a/activerecord/lib/active_record/associations/through_association.rb +++ b/activerecord/lib/active_record/associations/through_association.rb @@ -10,8 +10,20 @@ module ActiveRecord protected + # We merge in these scopes for two reasons: + # + # 1. To get the scope_for_create on through reflection when building associated objects + # 2. To get the type conditions for any STI classes in the chain + # + # TODO: Don't actually do this. Getting the creation attributes for a non-nested through + # is a special case. The rest (STI conditions) should be handled by the reflection + # itself. def target_scope - super.merge(through_reflection.klass.scoped) + scope = super + through_reflection_chain[1..-1].each do |reflection| + scope = scope.merge(reflection.klass.scoped) + end + scope end def association_scope @@ -227,21 +239,18 @@ module ActiveRecord def reflection_conditions(index) reflection = through_reflection_chain[index] - conditions = through_conditions[index].dup - - # TODO: maybe this should go in Reflection#through_conditions directly? - unless reflection.klass.descends_from_active_record? - conditions << reflection.klass.send(:type_condition) - end + conditions = through_conditions[index] unless conditions.empty? - conditions.map! do |condition| - condition = reflection.klass.send(:sanitize_sql, interpolate(condition), reflection.table_name) - condition = Arel.sql(condition) unless condition.is_a?(Arel::Node) - condition - end + Arel::Nodes::And.new(process_conditions(conditions, reflection)) + end + end - Arel::Nodes::And.new(conditions) + def process_conditions(conditions, reflection) + conditions.map do |condition| + condition = reflection.klass.send(:sanitize_sql, interpolate(condition), reflection.table_name) + condition = Arel.sql(condition) unless condition.is_a?(Arel::Node) + condition end end diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index e3e2cac042..7ae9bfc928 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -416,6 +416,9 @@ module ActiveRecord else # If the source reflection does not go through another reflection, then we can get # to this reflection directly, and so start the chain here + # + # It is important to use self, rather than the source_reflection, because self + # may has a :source_type option which needs to be used. chain = [self] end |