0

这计算了代码通知为 的用户数true,他的个人资料notificationtrue

@count = User.joins(:profile, :codes => :community).where("profiles.notification=? AND codes.notification=? AND communities.id=?", true, true, @community.id).count

我正在使用调用acts_as_paranoid逻辑删除的 gem。有了这个,上面的行包括已删除的具有日期时间的记录user.deleted_at

我想排除那些已删除的记录。
我怎样才能在上面添加这个额外的条件?

像这样的东西?

@count = User.joins(:profile, :codes => :community).where("user.deleted_at=? AND profiles.notification=? AND codes.notification=? AND communities.id=?", nil, true, true, @community.id).count
4

2 回答 2

2

您也可以尝试在模型上使用范围:

class User < ActiveRecord::Base
    scope :not_deleted, where(:deleted_at => nil)
end

然后做:(注意User.not_deleted.joins...

@count = User.not_deleted.joins(:profile, :codes => :community).where("profiles.notification=? AND codes.notification=? AND communities.id=?", true, true, @community.id).count
于 2013-04-19T16:24:24.297 回答
1

That looks right to me, but user.deleted_at should be users.deleted_at.

- @count = User.joins(:profile, :codes => :community).where("user.deleted_at=? AND profiles.notification=? AND codes.notification=? AND communities.id=?", nil, true, true, @community.id).count
+ @count = User.joins(:profile, :codes => :community).where("users.deleted_at=? AND profiles.notification=? AND codes.notification=? AND communities.id=?", nil, true, true, @community.id).count

The SQL generated by Rails uses the plural table name (users) instead of the singular model name (user).

于 2013-04-19T16:17:04.923 回答