0

I am currently working on an app for sporting clubs, this allows club admins to manage the divisions, teams, and ultimately the players.

At present I have my database/relationships as follows

class Sport < ActiveRecord::Base
  has_many  :clubs
  has_many :teams
  attr_accessible :club_id


class Club < ActiveRecord::Base
  belongs_to :sport
  has_many  :teams
  has_many :users
  has_many  :divisions
  attr_accessible :sport_id

class Division < ActiveRecord::Base
  has_many :teams
  belongs_to  :club
  attr_accessible :club_id

class Team < ActiveRecord::Base
  has_many  :users
  has_many  :schedules
  belongs_to :division
  belongs_to :club
  attr_accessible :division_id, :club_id

class User < ActiveRecord::Base
  belongs_to :club
  belongs_to :team
  attr_accessiable :club_id, :sport_id

essential what I would like is a more concise way of managing this. i.e.

  • User can only belong to 1 club, within 1 division, but can have multiple teams
  • Team can only belong to 1 division, within 1 club, within 1 sport, but have multiple users
  • Division can only belong to 1 club, within 1 sport, but have multiple teams
  • Club can only belong to 1 sport, but have multiple teams, and multiple divisions

At present the above is working, but i dont think the relationships/structure is at its best

4

2 に答える 2

1

あなたの国にはスポーツクラブが1つしかありませんか? ここドイツでは、クラブの異なる部門が異なるスポーツを持つことができます (例えば、私のクラブは陸上競技、自転車競技、バレーボール、バスケットボール、卓球、水泳です)。私のアプリのほとんどでは、スポーツとディビジョンをスキップしています。スポーツはアプリのコンテキストから与えられており、クラブとディビジョンは同一のものと見なしています。私のアスレチック アプリは、クラブのディビジョンではなく、クラブだけを扱います。さもなければ複雑さが私を圧倒するので、私はこの犠牲を必要と考えています - もちろん、あなたのマイレージは異なるかもしれません:-)

さらに、ユーザーは一時的にクラブに所属するだけなので、ユーザーとチームの間には中間的な関係があります。ドイツ語で「Startberechtigung」、英語で「ライセンス」のようなものです。これは、属しているチームおよび属しているユーザーであり、追加の属性として「start_date」および「stop_date」があります。そうすれば、クラブやユーザーの履歴さえあります。記録リスト (再び: 陸上競技) にとって重要です!

その後、余分な推移的な関係を破棄します。ユーザーはチームに属し、チームはクラブに属します => has_many :through's more; を使用します。ユーザーの belongs_to club は必要ありません。

これで、モデルが少しきれいになったはずです。

更新

プレーヤーにクラス名「ユーザー」を使用しないでください。最終的にユーザー/管理者モデルとの競合が発生します。「選手」でも「アスリート」でも結構です。

更新 2 :

もちろん、あらゆる場合に :through は必要ありません。インクルードまたはクラスメソッドを含むスコープは、特にあなたにとって好ましいかもしれません。複数の中間クラスをブリッジしたい場合。

次のようなものはいかがですか(推奨ガイドの穴を埋めてください):

class Sport < ActiveRecord::Base

has_many :clubs
has_many :teams, :through => :clubs, :readonly => false


class Club < ActiveRecord::Base

belongs_to :sport
has_many :teams


class Team < ActiveRecord::Base

belongs_to :club

has_many :athlete_teams
has_many :athletes, :through => :athlete_teams, :readonly => false


class AthleteTeam < ActiveRecord::Base

belongs_to :teams
belongs_to :athletes


class Athlet < ActiveRecord::Base

has_many :athlete_teams
has_many :athletes, :through :athlete_teams, :readonly => false
于 2012-09-12T08:47:10.163 に答える
0

ActiveRecord アソシエーションに関するヘルプは、http://api.rubyonrails.orgにあります。

うまくいけば、これはあなたを助けるでしょう.

于 2012-09-12T08:07:45.800 に答える