mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add test case to has_many through association when mass_assignment_sanitizer is
:strict
This commit is contained in:
parent
c97a166691
commit
e9bf0e3157
4 changed files with 34 additions and 4 deletions
|
@ -4,6 +4,7 @@ require 'models/person'
|
|||
require 'models/reference'
|
||||
require 'models/job'
|
||||
require 'models/reader'
|
||||
require 'models/secure_reader'
|
||||
require 'models/comment'
|
||||
require 'models/tag'
|
||||
require 'models/tagging'
|
||||
|
@ -44,17 +45,33 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_associate_existing
|
||||
posts(:thinking); people(:david) # Warm cache
|
||||
post = posts(:thinking)
|
||||
person = people(:david)
|
||||
|
||||
assert_queries(1) do
|
||||
posts(:thinking).people << people(:david)
|
||||
post.people << person
|
||||
end
|
||||
|
||||
assert_queries(1) do
|
||||
assert posts(:thinking).people.include?(people(:david))
|
||||
assert post.people.include?(person)
|
||||
end
|
||||
|
||||
assert posts(:thinking).reload.people(true).include?(people(:david))
|
||||
assert post.reload.people(true).include?(person)
|
||||
end
|
||||
|
||||
def test_associate_existing_with_strict_mass_assignment_sanitizer
|
||||
ActiveRecord::Base.mass_assignment_sanitizer = :strict
|
||||
|
||||
SecureReader.new
|
||||
|
||||
post = posts(:thinking)
|
||||
person = people(:david)
|
||||
|
||||
assert_queries(1) do
|
||||
post.secure_people << person
|
||||
end
|
||||
ensure
|
||||
ActiveRecord::Base.mass_assignment_sanitizer = :logger
|
||||
end
|
||||
|
||||
def test_associate_existing_record_twice_should_add_to_target_twice
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
class Person < ActiveRecord::Base
|
||||
has_many :readers
|
||||
has_many :secure_readers
|
||||
has_one :reader
|
||||
|
||||
has_many :posts, :through => :readers
|
||||
has_many :secure_posts, :through => :secure_readers
|
||||
has_many :posts_with_no_comments, :through => :readers, :source => :post, :include => :comments,
|
||||
:conditions => 'comments.id is null', :references => :comments
|
||||
|
||||
|
|
|
@ -115,8 +115,10 @@ class Post < ActiveRecord::Base
|
|||
has_many :named_categories, :through => :standard_categorizations
|
||||
|
||||
has_many :readers
|
||||
has_many :secure_readers
|
||||
has_many :readers_with_person, :include => :person, :class_name => "Reader"
|
||||
has_many :people, :through => :readers
|
||||
has_many :secure_people, :through => :secure_readers
|
||||
has_many :single_people, :through => :readers
|
||||
has_many :people_with_callbacks, :source=>:person, :through => :readers,
|
||||
:before_add => lambda {|owner, reader| log(:added, :before, reader.first_name) },
|
||||
|
|
9
activerecord/test/models/secure_reader.rb
Normal file
9
activerecord/test/models/secure_reader.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
class SecureReader < ActiveRecord::Base
|
||||
self.table_name = "readers"
|
||||
|
||||
belongs_to :secure_post, :class_name => "Post", :foreign_key => "post_id"
|
||||
belongs_to :secure_person, :inverse_of => :secure_readers, :class_name => "Person", :foreign_key => "person_id"
|
||||
|
||||
|
||||
attr_accessible nil
|
||||
end
|
Loading…
Reference in a new issue