Rails: create on has_one association

后端 未结 4 1179
鱼传尺愫
鱼传尺愫 2020-12-22 23:05

Hi (huge Rails newbie here), I have the following models:

class Shop < ActiveRecord::Base
  belongs_to :user
  validates_uniqueness_of :title, :user_id, :         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-22 23:53

    Just to add to above answers -

    @user.create_shop(params[:shop])
    

    Above syntax creates new record but it subsequently deletes similar existing record.

    Alternatively, if you do not want to trigger delete callback

    Shop.create(user_id: user.id, title: 'Some unique title')
    

    This thread might be helpful. Click here

提交回复
热议问题