我會把8個座位的最大邏輯放到你的應用程序中,而不是你的數據庫。
你可能需要兩個表:classes
和seats
。由於class
是保留字,你應該使用類似SchoolClass:
class SchoolClass < ActiveRecord::Base
has_many :seats, :dependent => :destroy
end
class Seat < ActiveRecord::Base
belongs_to :school_class
end
要驗證的最多席位數量已經達到,過濾器和一個實例方法之前使用的組合。類似這樣的:
class SchoolClass
MAXIMUM_SEATS = 8
before_create :validate_limit
def limit_reached?
seats.count >= MAXIMUM_SEATS
end
private
def validate_limit
errors.add :base, "The maximum of this class has been reached" if limit_reached?
end
end
最後在年份和月份使用兩個整數列是一個好主意。你可能想在這些列添加一個索引爲好。