2

私は2つのモデルを持っています

# == Schema Information
#
# Table name: answers
#
#  id          :integer          not null, primary key
#  content     :text
#  question_id :integer
#  accept      :boolean
#  created_at  :datetime         not null
#  updated_at  :datetime         not null    
#  user_id     :integer
#

class Answer < ActiveRecord::Base
  attr_accessible :accept, :content, :question_id, :user
  belongs_to :question
  belongs_to :user
  delegate :username, to: :user, allow_nil: true, prefix: 'owner'
end

と質問

# == Schema Information
#
# Table name: questions
#
#  id           :integer          not null, primary key
#  title        :string(255)
#  content      :text             default(""), not null
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
#  user_id      :integer
#  viewed_count :integer          default(0)
#

class Question < ActiveRecord::Base

  validates_presence_of :title, :content, :user
  attr_accessible :content, :title, :tag_list
  acts_as_taggable
  belongs_to :user, :counter_cache => true
  has_many :answers
  delegate :username, to: :user, allow_nil: true, prefix: 'owner'
  scope :owner, joins(:user)
  scope :without_answer, joins(:answers).
     select('questions.id').
     group('questions.id').
     having('count(answers.id) = 0')

  validate :validation_of_tag_list

  def self.no_answer
    Question.all.select{|question|question.answers.count == 0}
  end

スコープwithout_answerとクラス メソッドは、no_answer理論的には同じである必要があります。ただし、以下のようにコンソールで実行します。

Loading development environment (Rails 3.2.13)
irb(main):001:0> Question.without_answer
  Question Load (0.6ms)  SELECT questions.id FROM `questions` INNER JOIN `answers` ON `answers`.`question_id` = `questions`.`id` GROUP BY questions.id HAVING count(answers.id) = 0
=> []
irb(main):002:0> Question.no_answer
  Question Load (0.6ms)  SELECT `questions`.* FROM `questions` 
   (0.5ms)  SELECT COUNT(*) FROM `answers` WHERE `answers`.`question_id` = 1
   (0.4ms)  SELECT COUNT(*) FROM `answers` WHERE `answers`.`question_id` = 2
   (0.4ms)  SELECT COUNT(*) FROM `answers` WHERE `answers`.`question_id` = 16
   (0.4ms)  SELECT COUNT(*) FROM `answers` WHERE `answers`.`question_id` = 17
   (0.3ms)  SELECT COUNT(*) FROM `answers` WHERE `answers`.`question_id` = 34
=> [#<Question id: 2, title: "Here is the second", content: "here you go\r\n", created_at: "2013-04-20 00:34:15", updated_at: "2013-04-20 00:34:15", user_id: nil, viewed_count: 0>, #<Question id: 16, title: "my question", content: "Here is my question", created_at: "2013-04-21 02:02:47", updated_at: "2013-04-23 02:29:27", user_id: 1, viewed_count: 1>, #<Question id: 17, title: "Hello", content: "me", created_at: "2013-04-23 00:37:56", updated_at: "2013-04-23 00:37:56", user_id: nil, viewed_count: 0>, #<Question id: 34, title: "Question title", content: "question content", created_at: "2013-04-23 04:57:49", updated_at: "2013-04-23 04:57:49", user_id: 42, viewed_count: 0>]
  1. スコープが期待どおりに機能しないのはなぜですか?
  2. そのような状況またはより良い解決策のためにどちらの方法が良いでしょうか?
4

1 に答える 1

7

スコープwithout_answerは非常に近いですが、次のような外部結合が必要です。

scope :without_answer,
   joins('LEFT OUTER JOIN answers ON answers.question_id = questions.id').
   select('questions.id').
   group('questions.id').
   having('count(answers.id) = 0')

次に、次のようにしてカウントを取得できますlength

Question.without_answer.length

注:without_answerと同じにするno_answer(つまり、実際の Question オブジェクトを返す) 場合は、. を削除する必要がありますselect

未回答の質問をカウントするより簡単で高速な方法は次のとおりです。

Question.joins('LEFT OUTER JOIN answers ON answers.question_id = questions.id').
         where('answers.id' => nil).count

また、これはそのままと同じものを返します。代わりに をno_answer使用するだけです。allcount

于 2013-12-15T05:09:07.030 に答える