aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2008-03-07 11:45:07 +0000
committerPratik Naik <pratiknaik@gmail.com>2008-03-07 11:45:07 +0000
commite1173500379ab35734ac9101e58d6eff6552ef6e (patch)
tree215fc1abeb3e005f6dbc01f4d639c18f43883f16 /activerecord
parent8a351648ab613d42bbbf44586cff8cdeccd181d4 (diff)
downloadrails-e1173500379ab35734ac9101e58d6eff6552ef6e.tar.gz
rails-e1173500379ab35734ac9101e58d6eff6552ef6e.tar.bz2
rails-e1173500379ab35734ac9101e58d6eff6552ef6e.zip
Add :readonly option to HasManyThrough associations. Closes #11156 [miloops]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8989 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb5
-rwxr-xr-xactiverecord/test/cases/associations_test.rb5
-rw-r--r--activerecord/test/models/author.rb3
4 files changed, 13 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 2a70952b34..0ee92e8ba9 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add :readonly option to HasManyThrough associations. #11156 [miloops]
+
* Improve performance on :include/:conditions/:limit queries by selectively joining in the pre-query. #9560 [dasil003]
* Perf fix: Avoid the use of named block arguments. Closes #11109 [adymo]
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 72bf693384..8c10ac641b 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -159,6 +159,7 @@ module ActiveRecord
:order => @reflection.options[:order],
:limit => @reflection.options[:limit],
:group => @reflection.options[:group],
+ :readonly => @reflection.options[:readonly],
:include => @reflection.options[:include] || @reflection.source_reflection.options[:include]
)
@@ -253,7 +254,9 @@ module ActiveRecord
:include => @reflection.options[:include],
:select => construct_select,
:order => @reflection.options[:order],
- :limit => @reflection.options[:limit] } }
+ :limit => @reflection.options[:limit],
+ :readonly => @reflection.options[:readonly],
+ } }
end
def construct_sql
diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb
index 89b978ad06..50643066b9 100755
--- a/activerecord/test/cases/associations_test.rb
+++ b/activerecord/test/cases/associations_test.rb
@@ -554,6 +554,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
companies(:first_firm).readonly_clients.find(:all).each { |c| assert c.readonly? }
end
+ def test_cant_save_has_many_readonly_association
+ authors(:david).readonly_comments.each { |c| assert_raise(ActiveRecord::ReadOnlyRecord) { c.save! } }
+ authors(:david).readonly_comments.each { |c| assert c.readonly? }
+ end
+
def test_triple_equality
assert !(Array === Firm.find(:first).clients)
assert Firm.find(:first).clients === Array
diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb
index 593d77342e..523d794fb3 100644
--- a/activerecord/test/models/author.rb
+++ b/activerecord/test/models/author.rb
@@ -20,7 +20,8 @@ class Author < ActiveRecord::Base
has_many :funky_comments, :through => :posts, :source => :comments
has_many :ordered_uniq_comments, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id'
has_many :ordered_uniq_comments_desc, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id DESC'
-
+ has_many :readonly_comments, :through => :posts, :source => :comments, :readonly => true
+
has_many :special_posts
has_many :special_post_comments, :through => :special_posts, :source => :comments