What is behind the "combining" of the Active Record method?

Is there a difference between syntaxes

"Game.joins(:round).where('rounds.league_id = 1')" 

and

 "Game.joins(:round).where(:rounds => {:league_id => 1})" 

The first produces a request:

 Game Load (7.5ms) SELECT "games".* FROM "games" INNER JOIN "rounds" ON "rounds"."id" = "games"."round_id" WHERE (rounds.league_id = 1) 

whereas the second produces a longer one:

 SQL (1.7ms) SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum WHERE a.attrelid = '"rounds"'::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum Game Load (8.8ms) SELECT "games".* FROM "games" INNER JOIN "rounds" ON "rounds"."id" = "games"."round_id" WHERE "rounds"."league_id" = 1 

Thanks in advance to everyone, I just joined the community, and this is my first question.

+4
source share
1 answer

In the second output log, you see no longer request, but instead ActiveRecord checks the circuit so that it can do the magic that it does. This reflection occurs very often in development, so any changes in the scheme are automatically selected, without worrying about restarting the processes (whether it is a console or web server).

If you look at the lines starting with Game Load , this is where the query interests you, and they are almost identical.

+4
source

Source: https://habr.com/ru/post/1400388/


All Articles