Rails one-to-one relationship

后端 未结 3 1236
攒了一身酷
攒了一身酷 2021-02-02 02:29

I have the following:

class User < ActiveRecord::Base
  has_one :car, :class_name => \'Car\', :foreign_key => \'user_id\'

class Car < ActiveRecord::         


        
3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-02 02:58

    why don't you just test before the user tries to add a car?

    if worker.car
     raise "sorry buddy, no car for you"
    else
     car = Car.create(user_id: worker.id)
    end
    

提交回复
热议问题