我的ability.rb文件中有以下定义:
can :index, Call, :country_id => user.countries
我正在尝试仅将其country_id位于当前用户的国家/地区数组中的呼叫编入索引。
这在我尝试时似乎有效:它只显示我被current_user所属的国家/地区标记的来电。
但是,我有以下测试,似乎无法通过:
before :each do
# Users
@admin = FactoryGirl.create(:user, admin: true)
end
it "should show the call index page with just calls that have the same country tags as them" do
anotheruser1 = FactoryGirl.create(:user)
anotheruserscall1 = FactoryGirl.create(:call, user_id: anotheruser1.id)
anotheruser2 = FactoryGirl.create(:user)
anotheruserscall2 = FactoryGirl.create(:call, user_id: anotheruser2.id)
@admin.countries << anotheruserscall1.country
ability = Ability.new(@admin)
ability.should be_able_to(:index, anotheruserscall1)
ability.should_not be_able_to(:index, anotheruserscall2)
end
但我失败了:
Failures:
1) Admin ability should show the call index page with just calls that have the same country tags as them
Failure/Error: ability.should be_able_to(:index, anotheruserscall1)
expected to be able to :index #<Call id: 1, subject: "Natus qui fuga ut.", description: "Consequatur aut veritatis earum dolor. Reprehenderi...", from: "2012-10-25 18:43:23", to: "2012-10-25 19:43:23", user_id: 7, status: "Tentative", created_at: "2012-10-29 13:41:07", updated_at: "2012-10-29 13:41:07", expert_id: nil, country_id: 1>
# ./spec/models/ability_spec.rb:291:in `block (2 levels) in <top (required)>'
非常感谢任何帮助。
答案 0 :(得分:0)
好的,我通过将详细信息作为块传递来修复此问题,如下所示:
can :index, Call, ["country_id IN (?)", user.countries] do |call|
user.countries.include?(call.country)
end