0

我有一个应用程序允许律师和法律学生回答法律问题。他们的答案可以投票。在views/question/show.html.erb 上的每个答案旁边,应用程序会指明答案是否已被投票以及由谁(律师或法学院学生)投票。但是,它的行为非常奇怪。目前,在测试问题上,如果律师对答案投了赞成票,则应用程序不会显示赞成票,但如果学生对答案投了赞成票,则将显示学生和律师的投票,但都显示为学生票。

这是 Questions 控制器的 show 操作中的代码,它检索一个问题的所有答案,然后查询每个答案的投票类型。

  def show

  @question = Question.find(params[:id])
  @answers = @question.answers

   @answers.each do |a|


       @lawyervotes = AnswerVote.where({:answer_id => a.id, :lawyervote => true}).reload
       puts @lawyervotes.inspect
       puts "lawyervotes"
       @studentvotes = AnswerVote.where({:answer_id => a.id, :studentvote => true}).reload
       @uservotes = AnswerVote.where({:answer_id => a.id, :lawyervote => nil, :studentvote => nil}).reload

    end 
  end

如果我在控制台中查看 puts 语句,它会显示 @lawyervotes 包含一个结果,但随后它突然变成了一个空数组。目前,这个问题有两个答案,这就是为什么 puts 语句运行了两次,但我不知道为什么第二次它是空的

[#<AnswerVote id: 34, value: 3, answer_id: 54, user_id: 37, created_at: "2013-05-08 18:29:34", updated_at: "2013-05-08 18:29:34", lawyervote: true, studentvote: nil>]
lawyervotes
[]
lawyervotes

请注意,我将每个查询放在末尾的原因reload是为了避免出现 ActiveRecord::AssociationTypeMismatch 错误,根据我发现的另一个 SO 答案,当您使用“where”查询时可能会发生这种错误。我发现另一个 SO 答案说在where查询末尾放置“重新加载”可以帮助避免该错误。

你能解释一下为什么我的律师票和学生票会发生这种奇怪的行为,并可能告诉我如何重写表演动作来避免它。先感谢您。

更新

这是控制台记录,显示问题 62 有两个答案,每个答案都有一个 answer_vote。其中一个答案投票是由律师(律师 = true),而一个是学生(学生 = true),但是,即使在尝试了 dmitry 的解决方案之后,他们都在我的申请中显示为学生投票。

>> q = Question.find_by_id(62)
  Question Load (0.2ms)  SELECT "questions".* FROM "questions" WHERE "questions"."id" = 62 LIMIT 1
=> #<Question id: 62, details: "I have a terminal illness but don't have time to go...", question: "What happens if I die without a will?", user_id: 35, accepted_answer_id: nil, created_at: "2013-05-08 18:19:48", updated_at: "2013-05-08 18:19:48", city: "Toronto", province: nil, province_id: 6>
>> q.answers
  Answer Load (0.2ms)  SELECT "answers".* FROM "answers" WHERE "answers"."question_id" = 62
=> [#<Answer id: 54, content: "There is legislation that determines the rules of i...", accepted: nil, user_id: 50, question_id: 62, created_at: "2013-05-08 18:20:41", updated_at: "2013-05-08 18:20:41">, #<Answer id: 55, content: "Ontario has statutory provisions that detail who in...", accepted: nil, user_id: 37, question_id: 62, created_at: "2013-05-08 18:22:53", updated_at: "2013-05-08 18:22:53">]
>> a54 = Answer.find_by_id(54)
  Answer Load (0.3ms)  SELECT "answers".* FROM "answers" WHERE "answers"."id" = 54 LIMIT 1
=> #<Answer id: 54, content: "There is legislation that determines the rules of i...", accepted: nil, user_id: 50, question_id: 62, created_at: "2013-05-08 18:20:41", updated_at: "2013-05-08 18:20:41">
>> a54.answer_votes
  AnswerVote Load (0.2ms)  SELECT "answer_votes".* FROM "answer_votes" WHERE "answer_votes"."answer_id" = 54
=> [#<AnswerVote id: 34, value: 3, answer_id: 54, user_id: 37, created_at: "2013-05-08 18:29:34", updated_at: "2013-05-08 18:29:34", lawyervote: true, studentvote: nil>]
>> a55 = Answer.find_by_id(55)
  Answer Load (0.3ms)  SELECT "answers".* FROM "answers" WHERE "answers"."id" = 55 LIMIT 1
=> #<Answer id: 55, content: "Ontario has statutory provisions that detail who in...", accepted: nil, user_id: 37, question_id: 62, created_at: "2013-05-08 18:22:53", updated_at: "2013-05-08 18:22:53">
>> a55.answer_votes
  AnswerVote Load (0.3ms)  SELECT "answer_votes".* FROM "answer_votes" WHERE "answer_votes"."answer_id" = 55
=> [#<AnswerVote id: 35, value: 3, answer_id: 55, user_id: 50, created_at: "2013-05-08 18:37:32", updated_at: "2013-05-08 18:37:32", lawyervote: nil, studentvote: true>]

更新

我把这段代码放在循环中

   puts AnswerVote.where({:answer_id => a.id}).reload.inspect
   puts "inspectinganswervote"

得到了这个结果

[#<AnswerVote id: 34, value: 3, answer_id: 54, user_id: 37, created_at: "2013-05-08 18:29:34", updated_at: "2013-05-08 18:29:34", lawyervote: true, studentvote: nil>]
inspectinganswervote
[#<AnswerVote id: 35, value: 3, answer_id: 55, user_id: 50, created_at: "2013-05-08 18:37:32", updated_at: "2013-05-08 18:37:32", lawyervote: nil, studentvote: true>]
inspectinganswervote

更新

答案.rb

class Answer < ActiveRecord::Base
  attr_accessible :accepted, :content, :question_id, :user_id

  has_many :comments
  belongs_to :question
  belongs_to :user
  has_many :answer_votes
  has_and_belongs_to_many :watchers, :join_table => "answer_watchers", :class_name => "User"
  has_reputation :votes, source: :user, aggregated_by: :sum
  has_reputation :lawyervotes, source: :user, aggregated_by: :sum
  has_reputation :studentvotes, source: :user, aggregated_by: :sum
  has_reputation :best, source: :user, aggregated_by: :sum
  # 


  def add_to_watchers(user)
    self.watchers << user unless self.watchers.include?(user)
  end 

    after_create :creator_watches_me
  private 

  def creator_watches_me
    self.watchers << user unless self.watchers.include?(user)
  end 


end

回答投票.rb

class AnswerVote < ActiveRecord::Base
  attr_accessible :answer_id, :user_id, :value, :answer, :lawyervote, :studentvote

  belongs_to :answer
  belongs_to :user

  validates_uniqueness_of :answer_id, scope: :user_id
  validates_inclusion_of :value, in: [1,-1,10,-10, 3]
  validate :ensure_not_author

 scope :lawyers, where(lawyervote: true)
scope :students, where(studentvote: true)


  def ensure_not_author
    errors.add :user_id, "is the author of the answer" if answer.user_id == user_id
  end

end
4

2 回答 2

3

其中一个问题——你在下一次迭代中重写了你的@lawyervotes 数组。一种解决方法是附加它(使用类似的东西:

@lawyervotes = []
@answers.each do |a|
  @lawyervotes <<= AnswerVote.where({:answer_id => a.id, :lawyervote => true}).reload
  ...
end

但它是超级糟糕的,非 Rails 风格。正如我上面提到的,您不需要通过@answers 进行此迭代,您只需编写:

更新

@lawyervotes = @question.answers.map {|a| a.answer_votes.lawyers}.reject!(&:empty?).flatten
@studentvotes = @question.answers.map {|a| a.answer_votes.students}.reject!(&:empty?).flatten

在您的 AnswerVotes 模型中:

scope :lawyers, where(lawyervote: true)
scope :students, where(studentvote: true)
于 2013-05-08T19:18:09.927 回答
0

对于第二个答案,您将lawyervotes数组设为空,因为第二个答案只有一个AnswerVote和:) 所以投票存在于变量中。laywervote = nilstudentvote = true@studentvotes

如果你也检查你@studentvotes的,你会在循环的第二次迭代中看到你的第二次投票。

于 2013-05-08T19:36:29.387 回答