具有多态关联的灯具不起作用

我正在尝试实现Rolify gem但是无法添加具有范围的灯具。 下面的(模型)测试的最后一行失败,因为目前主持人角色似乎全局给予@user ,而不是仅给予组织1。 下面的灯具没有使用resource_idresource_type ,它们在gem文件中提到了夹具,但我不知道如何使用它们。 如何将主持人角色的范围设置为仅限组织1?


roles.yml

 moderator: id: 1 resource: one (Organization) 

users.yml里

 one: email: example@example.com roles: moderator, organizations(:one) # I was hoping this would set the scope of the role to organization one but it isn't (seems to set the role globally). 

test.rb

 def setup @moderator_role = roles(:moderator) @organization1 = organizations(:one) @organization2 = organizations(:two) @user = users(:one) end test "should be moderator if fixtures correct" do assert_equal @user.has_role?('moderator'), true assert_equal @user.has_role?(:moderator, @organization1), true assert_equal @user.has_role?(:moderator, @organization2), false # This line fails end 

更新:我也试过下面的代码。 但是测试仍然失败了。

roles.yml

 moderator: name: :moderator resource: one (Organization) 

users.yml里

 one: organization: one roles: moderator, organizations(:one) 

organizations.yml

 one: name: "Company A" 

test.rb

 def setup @moderator_role = roles(:moderator) @organization1 = organizations(:one) @organization2 = organizations(:two) @user = users(:one) end test "should be moderator if fixtures correct" do assert_equal @user.has_role?('moderator'), true # This line fails assert_equal @user.has_role?(:moderator, @organization1), true # This line also fails assert_equal @user.has_role?(:moderator, @organization2), false end 

我发现使用更新中的代码,如果我将其作为用户控制器测试或集成测试运行,而不是作为模型测试,则测试确实通过。 所以我想我只是将它作为错误的测试类型运行。