15 lines
463 B
Ruby
15 lines
463 B
Ruby
class Project < ActiveRecord::Base
|
||
has_and_belongs_to_many :users
|
||
|
||
validate :require_at_least_on_user # 项目中最少要有一个用户
|
||
validates :name, presence: true, length: { minimum: 1, maximum: 50 }, uniqueness: true
|
||
|
||
private
|
||
|
||
def require_at_least_on_user
|
||
if !users || users.size == 0 # 这里使用size,可以智能选择需不需要查询数据库
|
||
errors[:users] = 'at least one user in the project'
|
||
end
|
||
end
|
||
end
|