0
我已將Rolify gem添加到我的項目併成功初始化它。我面臨一個奇怪的問題?Rolify has_role?方法返回false而不是true
用戶對象具有用戶和管理員角色。
[1] pry(#<API::V1::PostPolicy>)> current_user.roles
=> [#<API::V1::Role:0x007fb70602be48
id: "19810121-ea16-4d86-b36c-475fad0f7dec",
name: "user",
resource_id: nil,
resource_type: nil,
created_at: Thu, 02 Apr 2015 02:05:27 UTC +00:00,
updated_at: Thu, 02 Apr 2015 02:05:27 UTC +00:00>,
#<API::V1::Role:0x007fb705ee6858
id: "a51c59e2-9dd0-4838-b159-1af11f7db15d",
name: "admin",
resource_id: nil,
resource_type: nil,
created_at: Thu, 02 Apr 2015 02:05:27 UTC +00:00,
updated_at: Thu, 02 Apr 2015 02:05:27 UTC +00:00>]
當我查詢管理員角色時,我看到一個奇怪的行爲。
[2] pry(#<API::V1::PostPolicy>)> current_user.has_role? :admin
=> false
我想使用Pundit和Rolify執行策略,上面的輸出來自策略測試。
V
更新
我使用的UUID與rolify。 rolify的遷移如下。
class RolifyCreateAPIV1Roles < ActiveRecord::Migration
def change
create_table :api_v1_roles, id: :uuid do |t|
t.string :name
t.uuid :resource_id
t.string :resource_type
t.timestamps
end
create_table(:api_v1_users_api_v1_roles, :id => false) do |t|
t.uuid :user_id
t.uuid :role_id
end
add_index(:api_v1_roles, :name)
add_index(:api_v1_roles, [ :name, :resource_type, :resource_id ])
add_index(:api_v1_users_api_v1_roles, [ :user_id, :role_id ])
end
end
我明白了。嘗試'user.reload' – seoyoochan 2015-04-02 05:23:45
我曾嘗試過所有建議的方法。正如你懷疑的那樣,這可能與索引有關。我已經使用rolify表的遷移文件更新了我的帖子。 – fossil 2015-04-02 05:27:04
在桌子上,'api_v1_users_api_v1_roles',爲什麼ID是'false'?將其更改爲true,並刪除並創建表並在sqlite中遷移 – seoyoochan 2015-04-02 05:33:38