Sharing Query Logic Within ActiveRecord Models

04-Oct-2019 2120
I was recently working on an application in which we had a class method / scope used to return a specific set of users. This was fine and good until we found ourselves needing to capture the same logic at the instance level. Ideally, I was hoping to be able to avoid duplicating the logic, and simultaneously keep things efficient.Users in our application can be configured to receive alerts. We have a class method on the User model used to query for all the users who can receive alerts:.
Use coupon code:

RUBYONRAILS

to get 30% discount on our bundle!
Prepare for your next tech interview with our comprehensive collection of programming interview guides. Covering JavaScript, Ruby on Rails, React, and Python, these highly-rated books offer thousands of essential questions and answers to boost your interview success. Buy our 'Ultimate Job Interview Preparation eBook Bundle' featuring 2200+ questions across multiple languages. Ultimate Job Interview Preparation eBook Bundle