0
まず、私の悪い英語を申し訳ありません。まだ勉強してる。collection_selectを使用したvalidates_uniqueness_ofのRuby on Railsエラー
問題
has_many :registers
has_many :solutions, through : :registers
ソリューション
- :
は、私は私のDBで3つのテーブルを持っています210
has_many :problems, through : :registers
登録
belongs_to: problem
belongs_to :solution
システムがうまく機能しています。私はすべての3つのテーブルに新しいデータを挿入することができます。テーブル/モデルRegister
ためのビューで
、問題と解決策を選択するために、私はこのように、collection_selectを利用する:私はRegister
にこの検証を追加しようとすると、
= form_for @register do |f|
.field
= f.label :problem_id
= collection_select(:register, :problem_id, @problems, :id, :name, {}, { :multiple => false })
.field
= f.label :solution_id
= collection_select(:register, :solution_id, @courses, :id, :name, {}, { :multiple => false })
.field
= f.label :entry_at
= f.datetime_select :entry_at
.actions = f.submit
問題が表示されます。
validates_uniqueness_of :student_id , scope: :course_id
それから私は得る:
> undefined method `map' for nil:NilClass
> = collection_select(:register, :problem_id, @problems, :id, :name, {}, { :multiple => false })
そして私は理由を知らない。
だから、私は、コントローラによって検証を実行しようとしました:
def create
@register = Register.new(register_params)
problem_id = @register.problem_id
solution_id = @register.solution_id
if Register.exists?(['problem_id LIKE ? AND solution_id LIKE ?', problem_id, solution_id ])
render 'new'
else
@register.save
respond_with(@register)
end
end
しかし、エラーが残っています。
私は原因がcollection_selectだと信じていますが、解決方法はわかりません。
もう一度言って、私は3つのDBテーブルすべてに日付を残すことができます。しかし、重複を避けようとすると、エラーが表示されます。
? 'collection_select(:register、:problem_id、@problems、:id、:name、{}、{:multiple => false}) ' – Eric
@Eric。新しいRegisterController、def newに設定されています。 – Paulo