I’m developing an app with Rails 3.2.11 and I’m using Devise and CanCan.
I’m setting permissions in the ability.rb file so the user can only manage items that belong to his company, and everything seems to work just fine.
- My User model
belongs toa Company model. - This Company can
have manyClients models.
I was wondering if there is a way to automate my queries with a scope or gem or whatever so every time I fetch via Client.all it only returns the clients that belong to the user’s company, or if I call Order.all it shows only de orders for his company clients.
Is this posible? If not, what would be the best approach?
Thank you very much in advance.
I hope I’m not misunderstanding, but what about writing a method?
If you want to fetch the users clients (which belong to the users company)
user.rb
in your controller you could simply:
and the same goes for orders
Thanks to SybariteManoj, he pointed out that (assuming your queries are not more complex):