如何与许多人合作有很多关系

时间:2014-02-22 09:01:41

标签: ruby-on-rails ruby

我有一个系统,您可以投票给属于某个团队的玩家。我还有一个展示位置模型,可以跟踪每个球员在特定月份的投票和每个球员的位置。

class Team < ActiveRecord::Base
  has_many :players, dependent: :destroy
  has_many :placements, through: :players

class Player < ActiveRecord::Base
  belongs_to :team
  has_many :votes, dependent: :destroy
  has_many :placements

class Placement < ActiveRecord::Base
  belongs_to :player
  attr_accessible :month, :player_id, :points, :position

class Vote < ActiveRecord::Base
  belongs_to :player
  attr_accessible :phone_id, :player_id

要检索'排行榜',我会这样做:

@leaderboard = @team.placements.includes(:player).where(month: Date.today.beginning_of_month).order('points desc')

我想介绍一个游泳池的想法,不同的团队可以加入游泳池(类似于幻想联盟)。这样我就可以不仅展示球员在球队中的位置,还可以展示球员在特定球场中的位置。

对此进行建模以及检索池的排行榜的正确/最佳方法是什么?

class Team < ActiveRecord::Base
 has_many :players, dependent: :destroy
 has_many :placements, through: :players
 has_many :pools, through: :team_pools

class Placement < ActiveRecord::Base
 belongs_to :player
 attr_accessible :month, :player_id, :points, :position

class Pool < ActiveRecord::Base
 has_many :teams, through: :team_pools

class TeamPool < ActiveRecord::Base
 belongs_to :team
 belongs_to :pool

@pool_leaderboard = ?

1 个答案:

答案 0 :(得分:2)

class Pool < ActiveRecord::Base
 has_many :teams, through: :team_pools
 has_many :placements, through => :teams

@pool_leaderboard = @pool.placements.includes(:player).where(month: Date.today.beginning_of_month).order('points desc')