3
我寫了這個數的答覆數量的關聯(至後)計算唯一記錄:如何通過獨特的用戶通過使用Rails的一個範圍3
p = Post.find 1
r = p.responses.count(:user_id, distinct: true)
我試圖將其轉換成範圍,但它拋出一個錯誤:undefined method 'default_scoped?' for 30:Fixnum
class Response < ActiveRecord::Base
belongs_to :author, class_name: 'User', foreign_key: 'user_id'
belongs_to :post
scope :by_unique_users, joins(:post).count(:user_id, distinct: true)
end
class Post < ActiveRecord::Base
belongs_to :user
has_many :responses
end
class User < ActiveRecord::Base
has_many :posts
has_many :responses
end
有趣的文章,但我認爲陪審團尚未被召喚在這個問題上。範圍仍然非常流行,恕我直言,比方法更好的語法。 – DGM
該文章於2011年4月21日更新,明確指出(a)錯誤是固定的,(b)作者使用範圍簡單的範圍 - 所以這只是大多數用戶的意見。 –
@DaveNewton我提到這個bug已經被修復了,但是感謝評論。我應該在某個時候重新訪問此答案,並可能完全忽略第一部分信息以避免混淆。 – Noz