ambition/app/models/project.rb

15 lines
463 B
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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