2013-02-23 56 views
1

你好我有一个人与城市之间的关系芯片。一个城市可以有很多人。Rails:模型限制

我想一个城市有最多十个人? 我怎么能约束呢?

+0

好像http://stackoverflow.com/questions/1611887/calling-custom-validation-methods-in-rails的副本可以执行自定义验证。 – kobaltz 2013-02-23 17:55:35

回答

1

下面是型号代码:

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