0

我希望在一行中选择所有QuizVersion模型信息和相关模型。Quiz

以下是我到目前为止所拥有的,它没有输出任何Quiz信息,只是QuizVersion信息:

控制器:

class QuizzesController < ApplicationController

    def getquiz

        @myquiz = QuizVersion.joins(:Quiz).find([14,16])
    end

end

测验模型:

class Quiz < ActiveRecord::Base
  set_table_name "quizzes"
  set_primary_key "quiz_ID"
  has_many :QuizVersions, :primary_key => "quiz_version_id", :foreign_key => "quiz_version_id"
end

测验版本型号:

class QuizVersion < ActiveRecord::Base
  # attr_accessible :title, :body
  set_primary_key "quiz_version_id"
  belongs_to :Quiz, :primary_key => "quiz_id", :foreign_key => "quiz_id"
end

这是结果信息:

---
- !ruby/object:QuizVersion
  attributes:
    quiz_version_id: 14
    quiz_id: 18
    quiz_name: Super Hero Quiz
    intro: Take this quiz to find out which super hero you are most like!
    email: ''
    activated: 0
    ip_address: 127.0.0.1
    public: 0
    quiz_type: ''
    created_time: 1347360590
- !ruby/object:QuizVersion
  attributes:
    quiz_version_id: 16
    quiz_id: 18
    quiz_name: Super Hero Quiz
    intro: Take this quiz to find out which super hero you are most like!
    email: ''
    activated: 0
    ip_address: 127.0.0.1
    public: 0
    quiz_type: ''
    created_time: 1347706841
4

2 回答 2

0

尝试:

@myquiz = Quiz.includes(:QuizVersions).find([14,16])
于 2013-04-12T13:25:00.490 回答
0

首先,通常使用小写来定义关联:

class Quiz < ActiveRecord::Base
  set_table_name "quizzes"
  set_primary_key "quiz_ID"
  has_many :quiz_versions
end

class QuizVersion < ActiveRecord::Base
  # attr_accessible :title, :body
  set_primary_key "quiz_version_id"
  belongs_to :quiz
end

有了它,您可以使用:

QuizVersion.includes(:quiz).find([14,16])

希望有帮助

编辑:我承认我没有运行这个来仔细检查......

于 2013-04-12T13:29:27.383 回答