diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-02-09 19:37:05 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-02-09 19:37:05 +0000 |
commit | b3065a51a927ded6b22835837598e92df606eaed (patch) | |
tree | 5436f083affe38e328447bd2187cf7020524b920 /activerecord/lib/active_record | |
parent | c326851e4db8a41d76cf05a8db71bc853fa08821 (diff) | |
download | rails-b3065a51a927ded6b22835837598e92df606eaed.tar.gz rails-b3065a51a927ded6b22835837598e92df606eaed.tar.bz2 rails-b3065a51a927ded6b22835837598e92df606eaed.zip |
Polymorphic join support for has_one associations (has_one :foo, :as => :bar). Closes #3785.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3558 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/has_one_association.rb | 10 |
2 files changed, 9 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 6b461b5514..0dc03487cb 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -899,7 +899,7 @@ module ActiveRecord def create_has_one_reflection(association_id, options) options.assert_valid_keys( - :class_name, :foreign_key, :remote, :conditions, :order, :include, :dependent, :counter_cache, :extend + :class_name, :foreign_key, :remote, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as ) reflection = create_reflection(:has_one, association_id, options, self) diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index 17483305fc..4dbc0f6441 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -66,9 +66,15 @@ module ActiveRecord end def construct_sql - @finder_sql = "#{@reflection.table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}" + case + when @reflection.options[:as] + @finder_sql = + "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " + + "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = '#{ActiveRecord::Base.send(:class_name_of_active_record_descendant, @owner.class).to_s}'" + else + @finder_sql = "#{@reflection.table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}" + end @finder_sql << " AND (#{sanitize_sql(@reflection.options[:conditions])})" if @reflection.options[:conditions] - @finder_sql end end end |