Created
September 20, 2014 13:12
-
-
Save chichunchen/e987a7fe6971996acbf4 to your computer and use it in GitHub Desktop.
Basketball-Box Database association
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
team.rb | |
class Team < ActiveRecord::Base | |
validates :name, :presence => true | |
has_many :players | |
has_many :games | |
end | |
---------------------------------------------------- | |
player.rb | |
class Player < ActiveRecord::Base | |
after_create :set_default_association | |
belongs_to :team | |
has_one :statistic, dependent: :destroy | |
has_many :game_statistic, dependent: :destroy | |
private | |
def set_default_association | |
self.create_statistic | |
end | |
end | |
---------------------------------------------------- | |
game.rb | |
class Game < ActiveRecord::Base | |
after_create :set_default_association | |
validates :rival, :presence => true | |
belongs_to :team | |
belongs_to :category | |
has_many :game_statistics | |
def set_default_association | |
# players game_statistic db create automatically | |
end | |
end | |
---------------------------------------------------- | |
statistics.rb | |
class Statistic < ActiveRecord::Base | |
belongs_to :player | |
end | |
---------------------------------------------------- | |
game_statistics.rb | |
class GameStatistic < ActiveRecord::Base | |
belongs_to :game | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
遇到困難:
新增比賽時,要能自動產生每個球員的逐場數據(game_statistic),原本是想要跟設計球員資料庫時一樣,用after_create自動將statistic生成,但是在game.rb中不知道要怎麼讓程式知道比賽中(隊伍中)有多少個球員,進而生成該隊所有成員的逐場數據資料庫。
除了以上的問題外,如果各位前輩覺得資料庫的關聯哪裡做修正會更好,也請大家指教~