设置正确的模型关联Rails 3

时间:2013-05-12 14:31:20

标签: ruby ruby-on-rails-3 model associations

我试图弄清楚这里最好的路径,之前的一个问题并没有对这个问题有所了解,但这可能是由于我的解释,所以我要尝试将其分解成更小的块以希望收集一些在用bitesized块学习的同时更多地了解这个问题

适用范围

足球预测应用程序,用户在即将到来的游戏中对得分进行预测。根据他们是否已正确预测,他们将获得一些积分。每个用户都可以创建一个团队,然后成为联盟的一部分。

当前模型

class User
  has_many predictions #prediction model has user_id as FK
  has_many :teams #team model has user_id as FK
  attr_accessible :prediction_id
end    

class Team   
  belongs_to :user
  attr_accessible :team_name, :user_id, :key
end

class Prediction
  has_many fixtures #to allow predictions from multiple users<br> 
  attr_accessible :home_team, :away_team, :home_score, :away_score, :fixture_date, :fixture_id, :user_id
end

class Result
  attr_accessible :home_team, :away_team, :fixture_date, :home_score, :away_score
end

我的问题是,我应该有一个单独的模型,例如指向存储从用户预测中获得的积分吗?因此,例如,如果用户正确地预测得分,则他们将被给予3分。

Point模型中,我将拥有属性

result_id, prediction_id, allocated_points

所以在这里我可以将预测与结果进行比较并分配点数。

任何值得赞赏的建议都非常难以理解。

2 个答案:

答案 0 :(得分:0)

我不认为这有正确/错误的答案。很难说没有看到每个模型的分析和属性,但我会从计算他们的观点开始:

class Predication
  def correct?
    fixture.result == result
  end
end

class User
  def points
    predications.select(&:correct?).size * 3
  end
end

class Team      
  def points
    users.map(&:points).inject(&:+)
  end
end

答案 1 :(得分:0)

我相信Point不应该是一个模特。它应该是用户表中的一列。 您应该根据预测和结果计算点,并更新相关用户的点。

但这只是我的观点,我可能完全错了。

相关问题