search

Home  >  Q&A  >  body text

ruby - 关于rails model的关系维护

我现在有两个model,House和Company是多对一的关系,那么我想在删除Company的时候,如果有House引用就不能删除,否则可以直接删除Conmpany,这种关系需要怎么配置呢?

大家讲道理大家讲道理2763 days ago626

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-21 11:17:58

    试试这样写行不

     has_many :houses, dependent: :restrict
    

    http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many

    :restrict this object raises an ActiveRecord::DeleteRestrictionError exception and cannot be deleted if it has any associated objects.

    reply
    0
  • Cancelreply