Rails_admin, List of models show zero when using cancan

I have models Owner, Shop and Item.

Owner has many Shops and Shops has many Items.

My ability on Cancan:

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

When I open my rails_admin dashboard, it says that I have zero Items and page List of Items is empty.

However, when I open Shops page, I can see all its Items and I can change them on Shops page.

When I write my code like this:

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

It throws me an error:

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

Why I can't list all my Items on Items List?


I have found one solution.

I need to add additinal field to my Item model:

field :shop_owner_id, type: String

Then I need to assign it:

 before_save :set_shop_owner_id

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

In my ability.rb file I needed to add this:

can :manage, Item, shop_owner_id: user.id

Which means, there is some kind of error when rails_admin wnats to index all items, which belongs to shop that belongs to current owner. This helps to avoid it.

Not good solution I think, but works.

Need Your Help

Align edge of element to center of superview in xCode/swift

ios xcode swift ios-autolayout

I found another stackoverflow post asking about essentially the exact same thing I am trying to solve (iOS Autolayout how to stretch 2 button horizontally), however I am still having issues figurin...

How to test devise mail delivery with devise, rspec, capybara, and mailspec

ruby-on-rails devise capybara mailer

I'm trying to test if devise is sending out confirmation emails. This is becomming to be a bit of a challenge to me because, the tests use a different environment in Rails and I'm not quite sure if...