diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index fc75662ebc67e..5fea56f6285e3 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Do not implicitly mark recordss of has_many :through as readonly but do mark habtm records as readonly (eventually only on join tables without rich attributes). [Marcel Mollina Jr.] + * Fixed broken OCIAdapter #4457 [schoenm@earthlink.net] 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 9bc29b049f52b..3f6fdb6bac9de 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 @@ -41,7 +41,7 @@ def find(*args) options[:conditions] = conditions options[:joins] = @join_sql - options[:readonly] ||= false + options[:readonly] ||= !options[:joins].nil? if options[:order] && @reflection.options[:order] options[:order] = "#{options[:order]}, #{@reflection.options[:order]}" diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index f6bee7b6aec85..8078b4113bfde 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1325,7 +1325,7 @@ def set_readonly_option!(options) #:nodoc: unless options.has_key?(:readonly) if scoped?(:find, :readonly) options[:readonly] = scope(:find, :readonly) - elsif !options[:joins].blank? + elsif !options[:joins].blank? && !options[:select] options[:readonly] = true end end @@ -2064,4 +2064,4 @@ def clone_attribute_value(reader_method, attribute_name) value end end -end \ No newline at end of file +end diff --git a/activerecord/test/readonly_test.rb b/activerecord/test/readonly_test.rb index 55f5cbe4ca9a3..584eb735fe19b 100755 --- a/activerecord/test/readonly_test.rb +++ b/activerecord/test/readonly_test.rb @@ -3,6 +3,8 @@ require 'fixtures/comment' require 'fixtures/developer' require 'fixtures/project' +require 'fixtures/reader' +require 'fixtures/person' # Dummy class methods to test implicit association scoping. def Comment.foo() find :first end @@ -50,19 +52,23 @@ def test_find_with_joins_option_implies_readonly def test_habtm_find_readonly dev = Developer.find(1) assert !dev.projects.empty? - dev.projects.each { |p| assert !p.readonly? } - dev.projects.find(:all) { |p| assert !p.readonly? } - dev.projects.find(:all, :readonly => true) { |p| assert p.readonly? } + assert dev.projects.all?(&:readonly?) + assert dev.projects.find(:all).all?(&:readonly?) + assert dev.projects.find(:all, :readonly => true).all?(&:readonly?) end def test_has_many_find_readonly post = Post.find(1) assert !post.comments.empty? - post.comments.each { |r| assert !r.readonly? } - post.comments.find(:all) { |r| assert !r.readonly? } - post.comments.find(:all, :readonly => true) { |r| assert r.readonly? } + assert !post.comments.any?(&:readonly?) + assert !post.comments.find(:all).any?(&:readonly?) + assert post.comments.find(:all, :readonly => true).all?(&:readonly?) end + def test_has_many_with_through_is_not_implicitly_marked_readonly + assert people = Post.find(1).people + assert !people.any?(&:readonly?) + end def test_readonly_scoping Post.with_scope(:find => { :conditions => '1=1' }) do