aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2006-04-01 20:03:10 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2006-04-01 20:03:10 +0000
commit53aa8da1a73bc283bc8b2e1469be9e0fe5855ab7 (patch)
treee2928874629c6265ee2ed2564d50ab0ad885adc9 /activerecord/lib/active_record/associations
parentbb2276098a5a21c35e5ee72b3097bd9d7caa919f (diff)
downloadrails-53aa8da1a73bc283bc8b2e1469be9e0fe5855ab7.tar.gz
rails-53aa8da1a73bc283bc8b2e1469be9e0fe5855ab7.tar.bz2
rails-53aa8da1a73bc283bc8b2e1469be9e0fe5855ab7.zip
Fixed that records returned from has_and_belongs_to_many associations with additional attributes should be marked as read only (fixes #4512) [DHH] DEPRECATED: Using additional attributes on has_and_belongs_to_many associations. Instead upgrade your association to be a real join model [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4123 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r--activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb12
1 files changed, 9 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
index 3f6fdb6bac..cd866d2cdd 100644
--- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -40,8 +40,8 @@ module ActiveRecord
end
options[:conditions] = conditions
- options[:joins] = @join_sql
- options[:readonly] ||= !options[:joins].nil?
+ options[:joins] = @join_sql
+ options[:readonly] = finding_with_ambigious_select?(options[:select])
if options[:order] && @reflection.options[:order]
options[:order] = "#{options[:order]}, #{@reflection.options[:order]}"
@@ -157,7 +157,13 @@ module ActiveRecord
@join_sql = "INNER JOIN #{@reflection.options[:join_table]} ON #{@reflection.klass.table_name}.#{@reflection.klass.primary_key} = #{@reflection.options[:join_table]}.#{@reflection.association_foreign_key}"
end
-
+
+ # Join tables with additional columns on top of the two foreign keys must be considered ambigious unless a select
+ # clause has been explicitly defined. Otherwise you can get broken records back, if, say, the join column also has
+ # and id column, which will then overwrite the id column of the records coming back.
+ def finding_with_ambigious_select?(select_clause)
+ !select_clause && @owner.connection.columns(@reflection.options[:join_table], "Join Table Columns").size != 2
+ end
end
end
end