如何计算复杂的条件?

时间:2013-04-18 19:32:49

标签: ruby-on-rails ruby-on-rails-3

社区 has_many:代码
代码 belongs_to:社区

用户 has_many:代码
代码 belongs_to:user

用户 has_one:个人资料 个人资料 belongs_to:user

@community.codes.users.count

这将返回其代码属于社区的用户数。

代码将名为visible的列作为布尔值 个人资料将名为notification的列设为布尔值

如果我只想计算那些属于社区的visible上代码为true的用户数,该怎么办?此外,user.profile.notification必须为true

有可能算上这个吗?

我的意思是,我想获得如下计算的计数数。
我想把它放在一行。

@community.codes.each do |code|
    if code.visible && code.user && code.user.profile.notification
        count = count + 1
    end
end

这仅解决了代码在visible上为真的用户的数量,该用户属于社区?但它忽略了这种情况user.profile.notification => true

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

0 个答案:

没有答案
相关问题