Advertisement
Guest User

Untitled

a guest
Jul 12th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement