19636


Rails4.1で

[code]

    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
    
[/code]

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

[code]

    Question.includes(:translations, {answers: :translations})
    
[/code]

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


CC BY-SA 3.0
2015-12-03T02:59:22.380
19582
2015-12-04T14:07:03.207
2015-12-03T03:14:43.403
9210
9210
question
ruby-on-rails rails-activerecord
1:N:Mのネストしたincludeでeager_loadingする方法


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

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

[code]

    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]]
    
[/code]

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

## 追記 2015-12-04 20:55

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

[code]

    -------------------------
    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
    
[/code]


CC BY-SA 3.0
2015-12-03T14:48:26.897
19602
2015-12-04T11:57:20.910
2015-12-04T11:57:20.910
7471
7471
19582
answer


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

[code]

    Question.eager_load(:translations, {answers: :translations})
    
[/code]

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


CC BY-SA 3.0
2015-12-04T14:01:54.427
19636
2015-12-04T14:07:03.207
2015-12-04T14:07:03.207
9008
9008
19582
answer