2017-01-14 8 views
0

'を介しにhas_many' から余分な属性値を取得します。私のanswerモデルで私は、以下の構造を有する関連

を:

class Answer < ApplicationRecord 
    has_many :answer_test_case_results 
    has_many :test_cases_result, through: :answer_test_case_results, source: :test_case 
end 

マイanswer_test_case_result

class AnswerTestCaseResult < ApplicationRecord 
    belongs_to :answer 
    belongs_to :test_case 

    def get_output 
    output 
    end 
end 

マイanswer_test_case_resultモデルがあり余分な属性は、outputという名前です。私のanswerモデルではoutputと私のtest_cases_resultという関係からアクセスしたいと思いますが、この属性は、test_caseオブジェクトのみが保存され、この回答に関連付けられています。

私のAnswerTestCaseResult(つまり、AnswerTestCaseResult.where(answer: answer, test_case: test_case))から直接クエリなしでoutputにアクセスする方法がありますか?

答えて

0

この種の操作の例が不足していることは信じられないほどです。しかし、今私は間違っていたことを理解しています:私はhas_many :answer_test_case_resultsにアクセスしてください。has_many :test_cases_result, through: :answer_test_case_results, source: :test_caseではありません。

そして、私は私の場合のために、属性を残しておきたい場合は、より適切な意味論的に、私が使用することができます。

has_many :test_cases_result, class_name: "AnswerTestCaseResult"は、だから私は、例えば、answer.test_cases_reult.first.outputを通じてoutputにアクセスすることができます。

関連する問題