{:l-6ren">
gpt4 book ai didi

ruby-on-rails - Active Record 的 'joins' 方法背后是什么?

转载 作者:行者123 更新时间:2023-12-04 03:54:34 27 4
gpt4 key购买 nike

语法有区别吗

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

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

第一个生成查询:

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

而第二个产生更长的一个:

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

在此先感谢大家,我刚刚加入社区,这是我的第一个问题

最佳答案

在第二个日志输出中,您看到的不是更长的查询,而是 ActiveRecord 检查模式,因此它可以发挥它的魔力。这种反射在开发中经常发生,因此对架构的任何更改都会自动获取,而不必担心重新启动您的进程(无论是控制台还是 Web 服务器)。

如果您查看以 Game Load 开头的行,这就是您感兴趣的查询发生的地方,它们几乎相同。

关于ruby-on-rails - Active Record 的 'joins' 方法背后是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9618981/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com