2012-12-16 51 views
0

在精簡項目,以說明問題Rails attribute_accessible允許在具有平行構造的2個模型中的1個模型中對屬性進行質量分配。爲什麼?

耙試驗得出:

1) Failure: 
test_can_mass_assign_accessible_flds(BrandTest) [ test/unit/brand_test.rb:11]: 

Exception raised: 
<#<ActiveModel::MassAssignmentSecurity::Error: \ 
    Can't mass-assign protected attributes: brand_name, support_num>>. 

2 tests, 2 assertions, 1 failures, 0 errors, 0 skips 

這兩個模型和測試有平行施工,但測試時產生不同的結果。經驗告訴我這應該是我的錯誤,但我看不到在哪裏。誰能幫忙?

型號:

class Brand < ActiveRecord::Base 
    attr_accessible :brand_name, :support_num, :comment 
end 

class Mask < ActiveRecord::Base 
    attr_accessible :brand_id, :brand_sku, :comment 
end 

測試:

require 'test_helper' 
class BrandTest < ActiveSupport::TestCase 
    def setup 
    @accessible_flds = { :brand_name => 'MyString', 
         :support_num => 'MyString', 
         :comment => 'MyText' } 
    end 

    test "can mass assign accessible flds" do 
    assert_nothing_raised { Mask.new(@accessible_flds) } 
    end 
end 

class MaskTest < ActiveSupport::TestCase 
    def setup 
    @accessible_flds = { :brand_id => 1, 
         :brand_sku => 'MyString', 
         :comment => 'MyText' } 
    end 

    test "can mass assign accessible flds" do 
    assert_nothing_raised { Mask.new(@accessible_flds) } 
    end 
end 

架構:

ActiveRecord::Schema.define(:version => 20121212024505) do 

    create_table "brands", :force => true do |t| 
    t.string "brand_name" 
    t.string "support_num" 
    t.text "comment" 
    end 

    create_table "masks", :force => true do |t| 
    t.integer "brand_id" 
    t.string "brand_sku" 
    t.text "comment" 
    end 

end 

恩vironment:

Windows 7 Pro, SP 1 
ruby -v =>> ruby 1.9.3p327 (2012-11-10) [i386-mingw32] 
rails -v =>> Rails 3.2.9 

回答

0

BrandTest類仍然引用Mask

Mask.new(@accessible_flds) 

應該

Brand.new(@accessible_flds) 
+0

腦門一巴掌!謝謝 – dpneumo

相關問題