2016-11-15 6 views
2

레일즈 5.0.0을 사용하고 있습니다. 나는이 모델belongs_to 필드를 선택 사항으로 만들려면 어떻게해야합니까?

class Scenario < ApplicationRecord 
    belongs_to :grading_rubric 
    has_many :confidential_memo 
end 

을 가지고 있지만 내가 모델 내 만들 방법을 호출 할 때, 그것은 내가 belongs_to는 것을 나타냅니다 어떻게 내가 오류가

full messages: ["Grading rubric must exist"] 

입니다

def create 
    @scenario = Scenario.new(scenario_params) 
    respond_to do |format| 
     if @scenario.save 
     puts "saved successfully." 
     format.html { redirect_to confidential_memo_path(@scenario), notice: 'Saved successfully.' } 
     else 
     puts "full messages: #{@scenario.errors.full_messages}" 
     format.html { render action: "show" } 
     end 
    end 
    end 

실패 인수는 선택적이어야합니다 (즉, null이 될 수 있습니까?).

답변

2

이전에 값을 nil으로 남겨두고 Rails가 완벽하게 만족할 수 있다는 것이 었습니다. 당신이 belongs_to는 선택 사양 일하려는 경우에는이 레일 5.

변경되었습니다, 당신은 단순히 optional: true을 통과해야 :

belongs_to :grading_rubric, optional: true 

당신은 그것을 here

에 대한 자세한 정보를 찾을 수 있습니다