Last active
December 25, 2015 04:09
-
-
Save sajoku/6914713 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Company < ActiveRecord::Base | |
| has_many :company_accounts | |
| has_many :users, through: :company_accounts | |
| end | |
| class User < ActiveRecord::Base | |
| has_many :company_accounts | |
| has_many :companies, through: :company_accounts | |
| end | |
| #has a user_id, company_id and a manager column. If manager is set the user is able to edit the company. | |
| class CompanyAccount < ActiveRecord::Base | |
| belongs_to :user | |
| belongs_to :company | |
| end | |
| #Enter CanCan, in the ability class I'm now doing: | |
| can :read, Company do |company| | |
| company.users.include?(user) && user.company_accounts.where(company_id: company.id).first.manager | |
| end | |
| # I would like to be able to do something like this: | |
| can :read, Company, company_account: {user_id: user.id} | |
| #this returns | |
| "SELECT `companies`.* FROM `companies` WHERE `company_account`.`user_id` = 1" | |
| #Update: | |
| #I'm able to do something like this: | |
| can :manage, Company, Company.joins(:company_accounts => :user).where(:company_accounts => { :user_id => user.id, :manager => true }) | |
| # This doesn't raise a cancan exception though. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment