Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

HABTM finder sets :readonly => false. Closes #2525.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2694 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 7219e82fe600ff1268b9a89efc7a289ac0108592 1 parent 07bff0b
@jeremy jeremy authored
View
2  activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*1.12.1* (October 19th, 2005)
+* HABTM finders shouldn't return readonly records. #2525 [patrick@lenz.sh]
+
* Make all tests runnable on their own. #2521. [Blair Zajac <blair@orcaware.com>]
* Always parenthesize :conditions options so they may be safely combined with STI and constraints.
View
1  activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -45,6 +45,7 @@ def find(*args)
end
options[:conditions] = conditions
options[:joins] = @join_sql
+ options[:readonly] ||= false
if options[:order] && @options[:order]
options[:order] = "#{options[:order]}, #{@options[:order]}"
View
8 activerecord/lib/active_record/base.rb
@@ -357,7 +357,7 @@ class << self # Class methods
# * <tt>:offset</tt>: An integer determining the offset from where the rows should be fetched. So at 5, it would skip the first 4 rows.
# * <tt>:joins</tt>: An SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id". (Rarely needed).
# The records will be returned read-only since they will have attributes that do not correspond to the table's columns.
- # Use <tt>find_by_sql</tt> to circumvent this limitation.
+ # Pass :readonly => false to override.
# * <tt>:include</tt>: Names associations that should be loaded alongside using LEFT OUTER JOINs. The symbols named refer
# to already defined associations. See eager loading under Associations.
# * <tt>:select</tt>: By default, this is * as in SELECT * FROM, but can be changed if you for example want to do a join, but not
@@ -384,8 +384,10 @@ class << self # Class methods
def find(*args)
options = extract_options_from_args!(args)
- # :joins implies :readonly => true
- options[:readonly] = true if options[:joins]
+ # :joins implies :readonly => true if unset.
+ if options[:joins] and !options.has_key?(:readonly)
+ options[:readonly] = true
+ end
case args.first
when :first
View
38 activerecord/test/readonly_test.rb
@@ -1,31 +1,41 @@
require 'abstract_unit'
-require 'fixtures/topic'
+require 'fixtures/developer'
+require 'fixtures/project'
class ReadOnlyTest < Test::Unit::TestCase
- fixtures :topics
+ fixtures :developers, :projects, :developers_projects
def test_cant_save_readonly_record
- topic = Topic.find(:first)
- assert !topic.readonly?
+ dev = Developer.find(:first)
+ assert !dev.readonly?
- topic.readonly!
- assert topic.readonly?
+ dev.readonly!
+ assert dev.readonly?
assert_nothing_raised do
- topic.content = 'Luscious forbidden fruit.'
+ dev.name = 'Luscious forbidden fruit.'
+ assert !dev.save
+ dev.name = 'Forbidden.'
end
-
- assert_raise(ActiveRecord::ReadOnlyRecord) { topic.save }
- assert_raise(ActiveRecord::ReadOnlyRecord) { topic.save! }
+ assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save }
+ assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save! }
end
def test_find_with_readonly_option
- Topic.find(:all).each { |t| assert !t.readonly? }
- Topic.find(:all, :readonly => false).each { |t| assert !t.readonly? }
- Topic.find(:all, :readonly => true).each { |t| assert t.readonly? }
+ Developer.find(:all).each { |d| assert !d.readonly? }
+ Developer.find(:all, :readonly => false).each { |d| assert !d.readonly? }
+ Developer.find(:all, :readonly => true).each { |d| assert d.readonly? }
end
def test_find_with_joins_option_implies_readonly
- Topic.find(:all, :joins => '').each { |t| assert t.readonly? }
+ Developer.find(:all, :joins => '').each { |d| assert d.readonly? }
+ Developer.find(:all, :joins => '', :readonly => false).each { |d| assert !d.readonly? }
+ end
+
+ def test_habtm_find_readonly
+ dev = Developer.find(:first)
+ 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? }
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.