Ruby on rails 3.2 与书写方法相比,复杂的模型关系

Ruby on rails 3.2 与书写方法相比,复杂的模型关系,ruby-on-rails-3.2,model-associations,Ruby On Rails 3.2,Model Associations,我正在为一个应用程序添加一个投票和业力/声誉系统(类似于这里,HN等),并决定制作我自己的,因为现有的gems没有我想要的所有功能。在处理基本功能时,我在模型中编写了一些相当复杂的关联,我不确定这与编写自己的方法(或作用域)来返回相同的数据相比如何 下面是伪代码。(用户可以对问题和答案进行投票,但用户也可以通过他们发布的Q/A进行向上投票和向下投票) 用户 有很多问题吗 有很多答案吗 你有很多票吗 有很多:问题投票,通过::问题,来源::投票, 条件:{voteable_type::questi

我正在为一个应用程序添加一个投票和业力/声誉系统(类似于这里,HN等),并决定制作我自己的,因为现有的gems没有我想要的所有功能。在处理基本功能时,我在模型中编写了一些相当复杂的关联,我不确定这与编写自己的方法(或作用域)来返回相同的数据相比如何

下面是伪代码。(用户可以对问题和答案进行投票,但用户也可以通过他们发布的Q/A进行向上投票和向下投票)

用户
有很多问题吗
有很多答案吗
你有很多票吗
有很多:问题投票,通过::问题,来源::投票,
条件:{voteable_type::question,'score>0'}
有很多:问题投票,通过::问题,来源::投票,
条件:{voteable_type::问题,'score<0'}
有很多:通过::答案,来源::投票,
条件:{voteable_type::answer,'score>0'}
通过::答案,来源::投票,
条件:{voteable_type::answer,'score<0'}
问题:
属于:用户
有很多问题吗
有很多票,如:可投票
答复
属于:用户
属于:问题
有很多票,如:可投票
投票
属于:投票人,类别名称:“用户”
属于:voteable,多态性:true
属于:用户,通过::voteable
属性可访问性:分数,:用户id,:可选项类型,:可选项id
具体来说,对于用户模型中的四个复杂关联,在用户模型或Q/A模型中编写一些返回相同信息的方法(或范围)是更好的性能、更不容易出错,还是具有其他优势


我对Rails(和编程)非常陌生,这是我的第一个堆栈溢出问题,非常感谢您的温柔。让我知道这个问题是否更适合不同的堆栈属性。谢谢

这是一个很好的问题,我也弄不明白。
User
  has_many :questions
  has_many :answers
  has_many :votes

  has_many :question_up_votes, through: :questions, source: :vote, 
            conditions: {voteable_type: :question, 'score > 0'}

  has_many :question_down_votes, through: :questions, source: :vote, 
            conditions: {voteable_type: :question, 'score < 0'}

  has_many :answer_up_votes, through: :answers, source: :vote, 
            conditions: {voteable_type: :answer, 'score > 0'}

  has_many :answer_down_votes, through: :answers, source: :vote, 
            conditions: {voteable_type: :answer, 'score < 0'}


Question
  belongs_to :user
  has_many :questions
  has_many :votes, as: :voteable


Answer
  belongs_to :user
  belongs_to :question
  has_many :votes, as: :voteable


Vote
  belongs_to :voter, class_name: "User"
  belongs_to :voteable, polymorphic: true
  belongs_to :user, through: :voteable

  attr_accessible :score, :user_id, :voteable_type, :voteable_id