aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb10
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb9
2 files changed, 18 insertions, 1 deletions
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))