aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2010-10-13 01:29:09 +0100
committerJon Leighton <j@jonathanleighton.com>2010-10-13 01:29:09 +0100
commit781ad0f8fee209bcf10c5e52daae246477d49ea7 (patch)
treec4b1c714f96bd2e29079bb59f7ee848e4ec9915b /activerecord/lib/active_record/associations.rb
parent199db8c8c006a5f3bcbbe2a32d39444a741c5843 (diff)
downloadrails-781ad0f8fee209bcf10c5e52daae246477d49ea7.tar.gz
rails-781ad0f8fee209bcf10c5e52daae246477d49ea7.tar.bz2
rails-781ad0f8fee209bcf10c5e52daae246477d49ea7.zip
First bit of support for habtm in through assocs - test_has_many_through_has_many_with_has_and_belongs_to_many_source_reflection now passes
Diffstat (limited to 'activerecord/lib/active_record/associations.rb')
-rw-r--r--activerecord/lib/active_record/associations.rb45
1 files changed, 35 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 41f882743c..2a72fa95c9 100644
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -2180,6 +2180,7 @@ module ActiveRecord
# to represent the join table)
table, join_table = table
+ # TODO: Can join_key just be reflection.primary_key_name ?
join_key = reflection.options[:foreign_key] ||
reflection.active_record.to_s.foreign_key
join_foreign_key = reflection.active_record.primary_key
@@ -2192,18 +2193,37 @@ module ActiveRecord
# We've done the first join now, so update the foreign_table for the second
foreign_table = join_table
+ # TODO: Can foreign_key be reflection.association_foreign_key?
key = reflection.klass.primary_key
foreign_key = reflection.options[:association_foreign_key] ||
reflection.klass.to_s.foreign_key
end
- elsif reflection.source_reflection.macro == :belongs_to
- key = reflection.klass.primary_key
- foreign_key = reflection.source_reflection.primary_key_name
-
- conditions << source_type_conditions(reflection, foreign_table)
else
- key = reflection.source_reflection.primary_key_name
- foreign_key = reflection.source_reflection.klass.primary_key
+ case reflection.source_reflection.macro
+ when :belongs_to
+ key = reflection.klass.primary_key
+ foreign_key = reflection.source_reflection.primary_key_name
+
+ conditions << source_type_conditions(reflection, foreign_table)
+ when :has_many, :has_one
+ key = reflection.source_reflection.primary_key_name
+ foreign_key = reflection.source_reflection.klass.primary_key
+ when :has_and_belongs_to_many
+ table, join_table = table
+
+ join_key = reflection.source_reflection.primary_key_name
+ join_foreign_key = reflection.source_reflection.klass.primary_key
+
+ relation = relation.join(join_table, join_type).on(
+ join_table[join_key].
+ eq(foreign_table[join_foreign_key])
+ )
+
+ foreign_table = join_table
+
+ key = reflection.klass.primary_key
+ foreign_key = reflection.source_reflection.association_foreign_key
+ end
end
conditions << table[key].eq(foreign_table[foreign_key])
@@ -2269,14 +2289,19 @@ module ActiveRecord
# For habtm, we have two Arel::Table instances related to a single reflection, so
# we just store them as a pair in the array.
- if reflection.macro == :has_and_belongs_to_many
+ if reflection.macro == :has_and_belongs_to_many ||
+ (reflection.source_reflection &&
+ reflection.source_reflection.macro == :has_and_belongs_to_many)
+
+ join_table_name = (reflection.source_reflection || reflection).options[:join_table]
+
aliased_join_table_name = alias_tracker.aliased_name_for(
- reflection.options[:join_table],
+ join_table_name,
table_alias_for(reflection, true)
)
join_table = Arel::Table.new(
- reflection.options[:join_table], :engine => arel_engine,
+ join_table_name, :engine => arel_engine,
:as => aliased_join_table_name
)