diff options
-rw-r--r-- | activerecord/CHANGELOG | 5 | ||||
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 10 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 9 | ||||
-rw-r--r-- | activerecord/test/associations_join_model_test.rb | 8 |
4 files changed, 31 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 296070b865..89b23c1e96 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,10 @@ *SVN* +* Raise error when trying to add to a has_many :through association. [Rick] + + @post.tags << @tag # BAD + @post.taggings.create(:tag => @tag) # GOOD + * Allow all calculations to take the :include option, not just COUNT (closes #4840) [Rick] * Update inconsistent migrations documentation. #4683 [machomagna@gmail.com] diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 4a637d5dab..2861a3940b 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -66,6 +66,16 @@ module ActiveRecord end end + class ReadOnlyAssociation < ActiveRecordError #:nodoc: + def initialize(reflection) + @reflection = reflection + end + + def message + "Can not add to a has_many :through association. Try adding to #{@reflection.through_reflection.name.inspect}." + end + end + module Associations # :nodoc: def self.append_features(base) super 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 44054b42e1..8cafb26d44 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -8,7 +8,6 @@ module ActiveRecord construct_sql end - def find(*args) options = Base.send(:extract_options_from_args!, args) @@ -41,6 +40,14 @@ module ActiveRecord @loaded = false end + def <<(*args) + raise ActiveRecord::ReadOnlyAssociation, @reflection + end + + [:push, :concat, :create, :build].each do |method| + alias_method method, :<< + end + protected def method_missing(method, *args, &block) if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method)) diff --git a/activerecord/test/associations_join_model_test.rb b/activerecord/test/associations_join_model_test.rb index 93cfd0084c..9bacbfc717 100644 --- a/activerecord/test/associations_join_model_test.rb +++ b/activerecord/test/associations_join_model_test.rb @@ -357,6 +357,14 @@ class AssociationsJoinModelTest < Test::Unit::TestCase assert_nil posts(:thinking).tags.find_by_name("General").attributes["tag_id"] end + def test_raise_error_when_adding_to_has_many_through + assert_raise(ActiveRecord::ReadOnlyAssociation) { posts(:thinking).tags << tags(:general) } + assert_raise(ActiveRecord::ReadOnlyAssociation) { posts(:thinking).tags.push tags(:general) } + assert_raise(ActiveRecord::ReadOnlyAssociation) { posts(:thinking).tags.concat tags(:general) } + assert_raise(ActiveRecord::ReadOnlyAssociation) { posts(:thinking).tags.build(:name => 'foo') } + assert_raise(ActiveRecord::ReadOnlyAssociation) { posts(:thinking).tags.create(:name => 'foo') } + end + private # create dynamic Post models to allow different dependency options def find_post_with_dependency(post_id, association, association_name, dependency) |