ã©ããã¦ã AR ã® query methods ã ãã§ã¯è¦æ±ãæºãããªãå ´åã®æå¾ã®æ段ã¨ãã¦ãfind_by_sql ã¡ã½ãããããã¾ããçãããã使ã£ã¦ãã¨æãã®ã§ãããæ»ãå¤ã®ã¬ã³ã¼ãããããã« association ããã¼ããããã¨ãã㨠N+1 ã«ãªãã±ã¼ã¹ãããã¾ãããã¨ãã£ã¦ User.preload(:comments).find_by_sql(sql) ã®ããã«ãã¦ãæå¾ éãã«ã¯åãã¾ãããåç´ã« preload é¨åãç¡è¦ãããã ãã§ãã ã§ã¯ã©ã®ããã«æ¸ãããããã§ããããã 次ã®ããã«æ¸ãã¨æå¾ éãã«åãã¾ã(Rails 4.2.1 ã§è©¦ãã¦ãã¾ã)ã records = User.find_by_sql(sql) ActiveRecord::Associations::Preloader.new.preload( records, :comments ) re
{{#tags}}- {{label}}
{{/tags}}