0

Rails4.1で

class Question < ActiveRecord::Base
  has_many :translations, class_name "QuestionTranslation"
  has_many :answers
end

class Answer < ActiveRecord::Base
  has_many :translations, class_name "AnswerTranslation"
end

という関係性がある場合に、

Question.includes(:translations, {answers: :translations})

とすると、AnswerTranslationeager_loadingしてくれません。
AnswerTranslationを含めてeager_loadingするにはどうしたらよいでしょうか?

0

2 件の回答 2

0

includesをやめて率直にeager_loadを使われてはどうでしょうか。

Question.eager_load(:translations, {answers: :translations})

includesは処理に応じて、自動で
eager_loadの挙動かpreloadの挙動に変わるので、
明確にeager_loadしたいのであれば、eager_loadを書いたほうがよいように思います。

0

該当するデータがない、ということはないでしょうか?

雑で恐縮ですが、テストを書いてみたら、eagar_load されました。

class QuestionTest < ActiveSupport::TestCase
  def setup
    Question.delete_all
    Answer.delete_all
    QuestionTranslation.delete_all
    AnswerTranslation.delete_all

    @q = Question.create
    @qt = @q.translations.create
    @a = @q.answers.create
    @at = @a.translations.create
  end

  test "valid test data" do
    assert_equal @q, Question.first
    assert_equal @a, Answer.first
    assert_equal @a.translations, [@at]
  end

  test "to_a" do
    q_list = Question.includes(:translations, answers: :translations).to_a
    assert q_list.first.answers.first.translations.loaded?
  end

  test "first" do
    q = Question.includes(:translations, answers: :translations).first
    assert q.answers.first.translations.loaded?
  end
end

  Question Load (0.1ms)  SELECT  "questions".* FROM "questions"   ORDER BY "questions"."id" ASC LIMIT 1
  Answer Load (0.1ms)  SELECT  "answers".* FROM "answers"   ORDER BY "answers"."id" ASC LIMIT 1
  AnswerTranslation Load (0.2ms)  SELECT "answer_translations".* FROM "answer_translations"  WHERE "answer_translations"."answer_id" = ?  [["answer_id", 980190963]]

delete_all しているのは、fixture を消すのが面倒だったからです。

追記 2015-12-04 20:55

スミマセン。貼ったログが足りませんでしたね。全てを貼ります。
どちらも eagar_load されていると思います。

-------------------------
QuestionTest: test_#first
-------------------------
  SQL (0.3ms)  DELETE FROM "questions"
  SQL (0.1ms)  DELETE FROM "answers"
  SQL (0.1ms)  DELETE FROM "question_translations"
  SQL (0.1ms)  DELETE FROM "answer_translations"
   (0.1ms)  SAVEPOINT active_record_1
  SQL (1.6ms)  INSERT INTO "questions" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", "2015-12-04 11:53:49.299013"], ["updated_at", "2015-12-04 11:53:49.299013"]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  SAVEPOINT active_record_1
  SQL (0.2ms)  INSERT INTO "question_translations" ("created_at", "question_id", "updated_at") VALUES (?, ?, ?)  [["created_at", "2015-12-04 11:53:49.325410"], ["question_id", 980190963], ["updated_at", "2015-12-04 11:53:49.325410"]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  SAVEPOINT active_record_1
  SQL (0.2ms)  INSERT INTO "answers" ("created_at", "question_id", "updated_at") VALUES (?, ?, ?)  [["created_at", "2015-12-04 11:53:49.331949"], ["question_id", 980190963], ["updated_at", "2015-12-04 11:53:49.331949"]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.2ms)  SAVEPOINT active_record_1
  SQL (0.4ms)  INSERT INTO "answer_translations" ("answer_id", "created_at", "updated_at") VALUES (?, ?, ?)  [["answer_id", 980190963], ["created_at", "2015-12-04 11:53:49.340173"], ["updated_at", "2015-12-04 11:53:49.340173"]]
   (0.2ms)  RELEASE SAVEPOINT active_record_1
  Question Load (0.2ms)  SELECT  "questions".* FROM "questions"   ORDER BY "questions"."id" ASC LIMIT 1
  QuestionTranslation Load (0.2ms)  SELECT "question_translations".* FROM "question_translations"  WHERE "question_translations"."question_id" IN (980190963)
  Answer Load (0.1ms)  SELECT "answers".* FROM "answers"  WHERE "answers"."question_id" IN (980190963)
  AnswerTranslation Load (0.1ms)  SELECT "answer_translations".* FROM "answer_translations"  WHERE "answer_translations"."answer_id" IN (980190963)
   (1.0ms)  rollback transaction
   (0.1ms)  begin transaction
------------------------
QuestionTest: test_#to_a
------------------------
  SQL (0.3ms)  DELETE FROM "questions"
  SQL (0.1ms)  DELETE FROM "answers"
  SQL (0.1ms)  DELETE FROM "question_translations"
  SQL (0.1ms)  DELETE FROM "answer_translations"
   (0.0ms)  SAVEPOINT active_record_1
  SQL (0.3ms)  INSERT INTO "questions" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", "2015-12-04 11:53:49.358442"], ["updated_at", "2015-12-04 11:53:49.358442"]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  SAVEPOINT active_record_1
  SQL (0.2ms)  INSERT INTO "question_translations" ("created_at", "question_id", "updated_at") VALUES (?, ?, ?)  [["created_at", "2015-12-04 11:53:49.361845"], ["question_id", 980190963], ["updated_at", "2015-12-04 11:53:49.361845"]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  SAVEPOINT active_record_1
  SQL (0.1ms)  INSERT INTO "answers" ("created_at", "question_id", "updated_at") VALUES (?, ?, ?)  [["created_at", "2015-12-04 11:53:49.364573"], ["question_id", 980190963], ["updated_at", "2015-12-04 11:53:49.364573"]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  SAVEPOINT active_record_1
  SQL (0.1ms)  INSERT INTO "answer_translations" ("answer_id", "created_at", "updated_at") VALUES (?, ?, ?)  [["answer_id", 980190963], ["created_at", "2015-12-04 11:53:49.367306"], ["updated_at", "2015-12-04 11:53:49.367306"]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
  Question Load (0.1ms)  SELECT "questions".* FROM "questions"
  QuestionTranslation Load (0.2ms)  SELECT "question_translations".* FROM "question_translations"  WHERE "question_translations"."question_id" IN (980190963)
  Answer Load (0.2ms)  SELECT "answers".* FROM "answers"  WHERE "answers"."question_id" IN (980190963)
  AnswerTranslation Load (0.2ms)  SELECT "answer_translations".* FROM "answer_translations"  WHERE "answer_translations"."answer_id" IN (980190963)
   (0.8ms)  rollback transaction
   (0.1ms)  begin transaction
----------------------------------
QuestionTest: test_valid_test_data
----------------------------------
  SQL (0.5ms)  DELETE FROM "questions"
  SQL (0.3ms)  DELETE FROM "answers"
  SQL (0.2ms)  DELETE FROM "question_translations"
  SQL (0.3ms)  DELETE FROM "answer_translations"
   (0.2ms)  SAVEPOINT active_record_1
  SQL (0.3ms)  INSERT INTO "questions" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", "2015-12-04 11:53:49.380211"], ["updated_at", "2015-12-04 11:53:49.380211"]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  SAVEPOINT active_record_1
  SQL (0.1ms)  INSERT INTO "question_translations" ("created_at", "question_id", "updated_at") VALUES (?, ?, ?)  [["created_at", "2015-12-04 11:53:49.382866"], ["question_id", 980190963], ["updated_at", "2015-12-04 11:53:49.382866"]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  SAVEPOINT active_record_1
  SQL (0.1ms)  INSERT INTO "answers" ("created_at", "question_id", "updated_at") VALUES (?, ?, ?)  [["created_at", "2015-12-04 11:53:49.385408"], ["question_id", 980190963], ["updated_at", "2015-12-04 11:53:49.385408"]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.0ms)  SAVEPOINT active_record_1
  SQL (0.1ms)  INSERT INTO "answer_translations" ("answer_id", "created_at", "updated_at") VALUES (?, ?, ?)  [["answer_id", 980190963], ["created_at", "2015-12-04 11:53:49.387952"], ["updated_at", "2015-12-04 11:53:49.387952"]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
  Question Load (0.1ms)  SELECT  "questions".* FROM "questions"   ORDER BY "questions"."id" ASC LIMIT 1
  Answer Load (0.1ms)  SELECT  "answers".* FROM "answers"   ORDER BY "answers"."id" ASC LIMIT 1
  AnswerTranslation Load (0.1ms)  SELECT "answer_translations".* FROM "answer_translations"  WHERE "answer_translations"."answer_id" = ?  [["answer_id", 980190963]]
   (0.7ms)  rollback transaction
1
  • ご丁寧にご回答ありがとうございます。 頂いた例だと逆にQuestionTranslationをeager_loadingしていないように見受けられるのですが、その点はいかがでしょうか? Question.includes(:translations, answers: :translations)で4つのModelを全部eager_loadingしてくれると思っておるのですが。
    – jay
    Commented 2015年12月4日 0:43

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。