I would like to be able to load an entire app so that I may find the descendants of a given class.
For example given I have the following class defined:
# app/models/foo_class.rb class FooClass < MySpecialBaseClass # pasta code end
It won't be found with:
irb> ObjectSpace.each_object.select { |obj| obj.is_a?(Class) && obj <= MySpecialBaseClass } => []
Until I call the const:
irb> FooClass
Then it is returned:
irb> ObjectSpace.each_object.select { |obj| obj.is_a?(Class) && obj <= MySpecialBaseClass } => [FooClass]
How would I go about doing this?
Conditional Eager Loading in Rails. One of the most common performance issues that can affect a rails application (or any other web application) is the n+1 queries problem. This is usually an easy issue to solve, but there may be situations where the solution is not so trivial.
Typically, when you want to use the eager loading feature you would use the #includes method, which Rails encouraged you to use since Rails2 or maybe even Rails1 ;). And that works like a charm doing 2 queries:
The whole mystery is that Rails has 2 ways of preloading data. One is using separate db queries to obtain the additional data. And one is using one query (with left join) to get them all.
The whole mystery is that Rails has 2 ways of preloading data. One is using separate db queries to obtain the additional data. And one is using one query (with left join) to get them all. If you use #preload, it means you always want separate queries. If you use #eager_load you are doing one query.
Well, after some digging, it actually is really simple. Just need to run the following.
Rails.application.eager_load!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With