对has_many关联进行Rails自定义验证

时间:2016-02-14 07:51:11

标签: ruby-on-rails validation associations

鉴于以下模型:

商业

class BusinessUser < ActiveRecord::Base
  belongs_to :business, polymorphic: true
  belongs_to :user

  enum role: {:owner => 0, :employee => 1, :admin => 2}

  validates_presence_of :user
  validates :user, uniqueness: { scope: :business, message: "Each user can only have one role" }

end

BusinessUser

class User < ActiveRecord::Base

  has_many :business_users
  has_many :businesses, through: :business_users, source_type: Business, source: :business

end

用户

02-14 03:39:29.569 30081 30096 E RecognitionService: call for recognition service without RECORD_AUDIO permissions

如何确保每个企业至少有一个具有角色的business_user:owner?

0 个答案:

没有答案