By: Saurav

2019-12-29 02:39:00 UTC

1. Look for uses of present?, none?, any?, blank? and empty? on objects which may be ActiveRecord::Relations. Are you just going to load the entire array later if the relation is present? If so, add load to the call (e.g. @my_relation.load.any?)

2. Be careful with your use of exists? - it ALWAYS executes a SQL query. Only use it in cases where that is appropriate - otherwise use present? or any other the other methods which use empty?

3. Be extremely careful using where in instance methods on ActiveRecord objects - they break preloading and often cause N+1s when used in rendering collections.

4. count always executes a SQL query - audit its use in your codebase, and determine if a size check would be more appropriate.


5. Gem to find Out Which Code Is Used In Production & Which Isn’t


6. Ruby for frontend: Ruby to JS compiler


7. Why my production app's memory increases over time?


8. Use of ensure helps deal with any error. The block of code you specify in it will always be executed even if there is an error.


9. How to get the integer value corresponding to a key from a rails enum of a class

Suppose you have an enum in a User class as shown below:

Class User
 user_type: {sales: 1, customer: 2, manager: 3, accounting: 4}

The user_type is an integer field on the User class. So, for Jim, who is a sales guy, 
u = User.find_by_name('Jim')

u.user_type will give 'sales'

To get the integer value 1 for Jim, you need to execute:


10. From ruby 2.5.1+, you need to use require 'bigdecimal' and require 'bigdecimal/util' to use to_d method


require 'bigdecimal'
require 'bigdecimal/util'


Owned & Maintained by Saurav Prakash

If you like what you see, you can help me cover server costs or buy me a cup of coffee though donation :)