Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Manager
- has_many :employees
- has_many :transactions, as: :transactable
- end
- class Employee
- belongs_to :manager
- has_many :transactions, as: :transactable
- end
- class Transaction
- belongs_to :transactable, polymorphic: true
- end
- class Transaction
- def self.belonging_transactions(manager)
- where(
- "(transactable_type = 'Manager' AND transactable_id = ?) OR (transactable_type = 'Employee' AND transactable_id = ?)",
- manager.id,
- manager.employees.ids
- )
- end
- end
- Transaction.belonging_transactions(manager) # here manager is Manager class object
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement