diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-08-19 14:56:58 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-08-19 14:56:58 +0000 |
commit | e96c58224c31c63b023b7f71e8f3ada440eb2fa8 (patch) | |
tree | 84335cb6ac8aa8c6cf5e2be29db646f0e7bd2de8 /activerecord/lib/active_record/associations/has_many_through_association.rb | |
parent | 81b05fd9098c492bcf8651fe9042e5157d9a994b (diff) | |
download | rails-e96c58224c31c63b023b7f71e8f3ada440eb2fa8.tar.gz rails-e96c58224c31c63b023b7f71e8f3ada440eb2fa8.tar.bz2 rails-e96c58224c31c63b023b7f71e8f3ada440eb2fa8.zip |
unbraindeadify addition to has_many :through
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4791 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/associations/has_many_through_association.rb')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 16 |
1 files changed, 12 insertions, 4 deletions
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 b5feeff1d2..0257b1857d 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -44,18 +44,26 @@ module ActiveRecord # must have ids in order to create records associating them, so this # will raise ActiveRecord::HasManyThroughCantAssociateNewRecords if # either is a new record. Calls create! so you can rescue errors. - def <<(*args) - return if args.empty? + # + # The :before_add and :after_add callbacks are not yet supported. + def <<(*records) + return if records.empty? through = @reflection.through_reflection raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) if @owner.new_record? + load_target + klass = through.klass klass.transaction do - args.each do |associate| + flatten_deeper(records).each do |associate| + raise_on_type_mismatch(associate) raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) unless associate.respond_to?(:new_record?) && !associate.new_record? - klass.with_scope(:create => construct_join_attributes(associate)) { klass.create! } + + @target << klass.with_scope(:create => construct_join_attributes(associate)) { klass.create! } end end + + self end [:push, :concat].each { |method| alias_method method, :<< } |