2015-07-28 42 views
2

我有模型Owner,ShopItemRails_admin,使用cancan時模型列表顯示爲零

Owner有很多ShopsShops有很多Items

我對慘慘能力:

can :manage, Shop, owner_id: user.id 
can :manage, Item, shop: {owner_id: user.id} 

當我打開我的rails_admin儀表板,它說,我有零ItemsList of Items頁是空的。

但是,當我打開Shops頁面時,我可以看到它的所有項目,並且我可以在Shops頁面上更改它們。

當我寫我的代碼是這樣的:

can :manage, Item do |item| 
    item.shop.owner_id == user.id 
    end 

這引發了我的錯誤:

The accessible_by call cannot be used with a block 'can' definition. The SQL cannot be determined for :index Item 

爲什麼我不能在我所有的ItemsItems List名單?

回答

0

我找到了一個解決方案。

我需要additinal字段添加到我的Item型號:

field :shop_owner_id, type: String 

然後,我需要爲它分配:

before_save :set_shop_owner_id 

def set_shop_owner_id 
    self.shop_owner_id = self.shop.owner.id.to_s 
end 

在我ability.rb文件,我需要補充一點:

can :manage, Item, shop_owner_id: user.id 

這意味着,當rails_admin wnats索引時會出現某種錯誤所有屬於屬於當前所有者的店鋪的物品。這有助於避免它。

不好的解決方案我認爲,但工程。