SHARE
TWEET

Untitled

a guest Jul 12th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Manager
  2.   has_many :employees
  3.   has_many :transactions, as: :transactable
  4. end
  5.  
  6. class Employee
  7.   belongs_to :manager
  8.   has_many :transactions, as: :transactable
  9. end
  10.  
  11. class Transaction
  12.   belongs_to :transactable, polymorphic: true
  13. end
  14.      
  15. class Transaction
  16.  
  17.   def self.belonging_transactions(manager)
  18.     where(
  19.       "(transactable_type = 'Manager' AND transactable_id = ?) OR (transactable_type = 'Employee' AND transactable_id = ?)",
  20.       manager.id,
  21.       manager.employees.ids
  22.     )
  23.   end
  24.  
  25. end
  26.      
  27. Transaction.belonging_transactions(manager) # here manager is Manager class object
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top