このところ、named_scope関係のバグの原因と対処法を調べるために ActiveRecordのEager Loadingの実装を調べていました。 ActiveRecordのEager Loadingは、 Rails2.1から、若干生成するSQLが変化しています。

Rails 2.1 Eager Loading

上記サイトでも紹介されているように、 今まではLEFT OUTER JOINを使って、 一回のクエリで全て読み込むようなSQLを生成していたのですが、 Rails2.1からは、複数回のクエリに分割するようになりました。

ということで、Eager Loadingを実際にやっているコードを調べてみたのですが、自分で好きなタイミングでEager Loadingを行う方法を見つけたので紹介します。

ActiveRecord::BaseクラスのprotectedメソッドActiveRecord.preload_associationsを利用します。

   1  articles = Article.all(:limit => 5)
   2  Article.send :preload_associations, articles, [:user => :face]

1行目は普通にEager LoadingせずにArticleリストを取得していますが、 二行目を実行することで、各ArticleのアソシエイションがEager Load されます。あとは、

   1  articles.first.user.face

などを実行してもSQLクエリは発行されません。 実質的に発行されるSQLは通常の:include を指定した場合と同じになるので、好きなタイミングでEager Load を実行できるようになったといえます。

posted by Png genki on Mon 4 Aug 2008 at 21:10

cloneしようとしたら、下記のエラーが出て失敗します。何度retryしても、VMWareのNATサービス再起動してもだめ。

   1  RA layer request failed: REPORT request failed on '/!svn/vcc/default': REPORT of '/!svn/vcc/default': Could not read response body: Secure connection truncated (https://svn.s21g.com) at /usr/local/bin/git-svn line 3833

ぐぐっても情報が乏しいのでわかりませんが、大量のログがあるcloneだとエラーになる気がします。

Make sure your connection did not timeout. This is usually a client side connection problem.
https://dev.openwrt.org/cgi-bin/trac.fcgi/ticket/3563

リビジョン番号を取得

s21gではsvnを使っています。

   1  svn log https://svn.s21g.com/blog/trunk | head -n 10

リビジョンを指定してclone

すぐにclone完了しました!

   1  git svn clone https://svn.s21g.com/blog/trunk blog.git -r 1798
   2  Checking out files: 100% (1632/1632), done.
   3  Checked out HEAD:
   4    https://svn.s21g.com/blog/trunk r1798

posted by Png satoko on Mon 4 Aug 2008 at 17:08

なんでClass#superclassはあるのに#subclassesはないんだろう?

   1  class Class
   2    def subclasses
   3      ret = []
   4      ObjectSpace.each_object(Class) do |cls|
   5        ret << cls if cls.superclass == self
   6      end
   7      ret
   8    end
   9  end

posted by Png technohippy on Mon 4 Aug 2008 at 01:42 with 5 comments
Contents
自分でEager Loadingが行われるタイミングを決める方法
git svn cloneに失敗する:git svn clone -r 1234で解決
Class#subclasses
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
KingofSmack: Here also good reads for this mobile applicatio... '14-5
Spencer: You don't have to re-compile it, this version w... '14-4
staiano: Any chance we can get a recompile for 10.9? '14-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ