31 lines
668 B
Ruby

class Table < ActiveRecord::Base
#TODO has_many :moderators
has_many :seats
has_many :users, through: :seats
has_many :games
#TODO: Deprecated current_game_id from database.
# Instead calculate with self.current_game
#
#has_one :current_game, :class_name => "Game", :primary_key => "current_game_id"
#TODO: Should these be part of Game initialize instead of callback?
after_create :add_seats
after_create :add_game
def current_game
self.games.last
end
private
def add_seats
1.upto(4) do |i|
self.seats.create(position: i)
end
end
def add_game
self.games.create if self.current_game_id.blank?
end
end