aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2008-03-21 18:09:03 +0000
committerRick Olson <technoweenie@gmail.com>2008-03-21 18:09:03 +0000
commit273b21faa911681ed4b6c748676146e0f6eed0a0 (patch)
tree7454ae8ead82fbe980c9f3630289c672f247b232 /activerecord/lib/active_record/associations
parentd8f76e66a19435239eec6f65f910133c6059cd60 (diff)
downloadrails-273b21faa911681ed4b6c748676146e0f6eed0a0.tar.gz
rails-273b21faa911681ed4b6c748676146e0f6eed0a0.tar.bz2
rails-273b21faa911681ed4b6c748676146e0f6eed0a0.zip
Add has_one :through support, finally. Closes #4756 [thechrisoshow]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9067 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r--activerecord/lib/active_record/associations/has_one_through_association.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb
new file mode 100644
index 0000000000..ecf9d8208d
--- /dev/null
+++ b/activerecord/lib/active_record/associations/has_one_through_association.rb
@@ -0,0 +1,28 @@
+module ActiveRecord
+ module Associations
+ class HasOneThroughAssociation < ActiveRecord::Associations::HasManyThroughAssociation
+
+ def create_through_record(new_value) #nodoc:
+ klass = @reflection.through_reflection.klass
+
+ current_object = @owner.send(@reflection.through_reflection.name)
+
+ if current_object
+ klass.destroy(current_object)
+ @owner.clear_association_cache
+ end
+
+ @owner.send(@reflection.through_reflection.name, klass.send(:create, construct_join_attributes(new_value)))
+ end
+
+ private
+ def find(*args)
+ super(args.merge(:limit => 1))
+ end
+
+ def find_target
+ super.first
+ end
+ end
+ end
+end