mysql - ActiveRecord custom has_many association makes multiple calls to the database -


I have a couple of active recorder objects, which are related to the I_many Association, having created custom with having_many associations. Example: < / P>

First AR item:

  class car < Vehicle has_many: Wheels, CLASS_NAME: "RoundObject", foreign_key: vehicle_id, status: "working = 1" validates_presence_of: wheels ... end   

second AR object: < Former> class round object

Please note that the above is not an indication of the function of my app, just to outline the relationship between my two AR objects.

The issue I am raising is that when I reset the cache (and thus my cache rails again in the database to all AR objects), when it is RoundObject object comes back, it gets a lot of calls, each SQL related to the collection of round object s for a vehicle_id Run is being done for the console being ordered, so it mer The output is kind of seen:. RoundObject load (2.0ms) round_objects` selection` * `round_objects` where` round_objects` with `.have__id` = 28 and (active = 1) RoundObject load (1.0ms) is selected from` Round_objects`. * `Round_objects` where` round_objects`.`vehicle_id` = 29 and (active = 1) RoundObject load (2.0ms) round_objects` selection `. * `Round_objects` where` round_objects`.`vehicle_id` = 30 and (active = 1)

My application has many other AR objects that have built-in any modification Without the has_many association, and I think that when they reset the cache, they used to hit the database only once, for example:

  Micropost load ( 15.0ms) microposts` selection `` `microposts` inside` join posts` on `posts`.`id` =` microposts`.`post_id`. My question is how can I hit the database once my AR object only caches reset. , While still need custom  has_many  association? Can I force myself to join on SQL queries manually, and will it help?  

Thank you!

You can use the method to include it Something like this:.

  Vehicle.where (conditions_for_getting_data) .includes (: round_object)    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -