2017-11-15 3 views
0

ところ、多くのイベントを持つユーザーレール - accepts_nested_attributes_forと存在検証、私は単純な関係を持っている私のアプリケーションで

class User < ApplicationRecord 
    has_many :events, foreign_key: 'created_by', dependent: :destroy 
    accepts_nested_attributes_for :events 
end 


class Event < ApplicationRecord 
    belongs_to :user, foreign_key: 'created_by', inverse_of: :events 
    validates :created_by, presence: true 
end 

私は私はいくつかのイベントで一緒にユーザーを作成しようとしています検証エラーが発生する"Companies.created byは空白にできません"

私のparamsハッシュは、以下のようになります。

{"user"=>{"events_attributes"=>[{"name"=>"disney show"}]}} 

私は予想通りvalidates :created_by, presence: trueすべての作品取り除きます。どんな助けもありがとう! inverse_of:を使用して、Userモデルに明示的に双方向の関連を指定する

+0

このフォームをレンダリングする場所からコントローラ機能を追加します。 – Satendra

答えて

0

試してみてください。

class User < ApplicationRecord 
    has_many :events, foreign_key: 'created_by', inverse_of: :user, dependent: :destroy 
    accepts_nested_attributes_for :events 
end 

そして、あなたのEventレコードがUserを指して存在する外部キーを使用して作成されます。

関連する問題