Ruby on rails 铁路:模型有限公司

Ruby on rails 铁路:模型有限公司,ruby-on-rails,model,Ruby On Rails,Model,你好,我有个人和城市的关系芯片。一个城市可以有很多人 我想一个城市最多有十个人? 我怎样才能控制它呢?您可以在模型中使用自定义验证器。看看这里:您可以在模型中使用自定义验证器。看看这里:这是型号代码: class Person belongs_to :city end class City has_many :persons validate_on_create :check_populations def check_populations return if pe

你好,我有个人和城市的关系芯片。一个城市可以有很多人

我想一个城市最多有十个人?
我怎样才能控制它呢?

您可以在模型中使用自定义验证器。看看这里:

您可以在模型中使用自定义验证器。看看这里:

这是型号代码:

class Person
  belongs_to :city
end

class City
  has_many :persons
  validate_on_create :check_populations

  def check_populations
    return if persons.length > 10
  end
end

以下是型号代码:

class Person
  belongs_to :city
end

class City
  has_many :persons
  validate_on_create :check_populations

  def check_populations
    return if persons.length > 10
  end
end
似乎是可以执行自定义验证的位置的副本。似乎是可以执行自定义验证的位置的副本。