モデル:Railsモデルでは、定義されていない場合、ロールバックを引き起こしますか?
class UserPosition < ApplicationRecord
belongs_to :user
belongs_to :job_title
end
UserPositionのスキーマ:
t.integer :user_id
t.integer :company_id
t.integer :industry_id
t.integer :department_id
t.integer :job_title_id
t.string :job_title_custom
user_positions_controller.rb
def create
@user_position = UserPosition.find_or_create_by(user_id: current_user.id)
@user_position.update_attributes({
:industry_id => params[:industry_id],
:department_id => params[:department_id],
:job_title_id => params[:job_title_id],
:job_title_custom => params[:job_title_custom]
})
私はレコードウィットを作成するかUserPositionを必要としますH:私はちょうどuser_id
& job_title_custom
でUserPositionを作成しようとするとそれは動作しません
user_id
job_title_custom
OR
t.integer :user_id
t.integer :company_id
t.integer :industry_id
t.integer :department_id
t.integer :job_title_id
現在、ログはROLLBACK
を表示するエラーメッセージは次のとおりです。
@messages={:job_title=>["must exist"]}
私はここで間違って何をしていますか? job_titleにはモデル内で定義された関係があるが、Railsガイドにはオプションであることが記載されているため、それが原因であると思われるので、わからない。ヘルプは高く評価しました
は、job_title_customです。 – AnApprentice
UserPositionの検証はありますか?もしそうなら、検証に失敗した場合、 'find_or_create_by'がロールバックします。そして私はあなたのコードから、ユーザーはuser_positionを1つだけ持つことができると仮定します。 – SteveTurczyn
これはRails 5アプリですか? job_titleテーブルのアソシエーションはbelongs_toアソシエーションですか? – hashrocket