我原来只有 Test 类
class Test< ActiveRecord::Base
...
end
但是我要增加一个类 SuperTest,如果这个类继承自 Test,就比较怪异,所以新增了一个基类 BaseTest,让两者都继承自 BaseTest
class BaseTest < ActiveRecord::Base
self.table_name = "tests"
belongs_to :project
end
class Test < BaseTest
belongs_to :super_test, class_name: "SuperTest", foreign_key: "test_id"
...
end
class SuperTest < BaseTest
has_many :tests, class_name: "Test", dependent: :destroy, foreign_key: "test_id"
...
end
class Project < ActiveRecord::Base
has_many :tests, dependent: :destroy
has_many :super_tests, dependent: :destroy
...
end
@test = Test.new(tests[:index0])
(rdb:1) tests[:index0]
{"source_project_id"=>"274", "source_branch"=>"master", "project_id"=>"108", "target_branch"=>"master"}
(rdb:1) @test.project
这个是能找到对应的 project 的
但是对于
@super_test = SuperTest.new(submodule_tests[:index0])
(rdb:1) @super_test .project
nil
另外,是否需要在 routes.rb 中增加 resources :super_tests?
补充: 跟了下代码,发现@super_test = SuperTest.new(submodule_tests[:index0]) 之后,@super_test的 project_id 字段还是 nil,所以@super_test.project也是 nil
[6, 15] in ruby/1.9.1/gems/activemodel-3.2.11/lib/active_model/mass_assignment_security/sanitizer.rb
6 def initialize(target=nil)
7 end
8
9 # Returns all attributes not denied by the authorizer.
10 def sanitize(attributes, authorizer)
=> 11 sanitized_attributes = attributes.reject { |key, value| authorizer.deny?(key) }
12 debug_protected_attribute_removal(attributes, sanitized_attributes)
13 sanitized_attributes
14 end
11 行 authorizer.deny?(key) 返回的都是 true,现在不明白为什么是 true,这个地方导致 sanitized_attributes 为 nil,所以最终赋值为 nil;
[24, 33] in ruby/1.9.1/gems/activemodel-3.2.11/lib/active_model/mass_assignment_security/permission_set.rb
24 end
25
26 class WhiteList < PermissionSet
27
28 def deny?(key)
=> 29 !include?(key)
30 end
31 end
结论:原来放在 Test 中的 attr_accessible 没有移到新增的基类中,所以导致字段赋值失败;