ruby on rails - How can I count with complicated conditions? -
community has_many :codes
code belongs_to :community
user has_many :codes
code belongs_to :user
user has_one :profile profile belongs_to :user
@community.codes.users.count this returns number of users code belonging community.
code has column called visible boolean
profile has column called notification boolean
what if want count number of users code true on visible, belonging community? also, user.profile.notification has true
is possible count that?
i mean, i'd obtain number of count that's calculated below.
want in 1 line.
@community.codes.each |code| if code.visible && code.user && code.user.profile.notification count = count + 1 end end this solves the number of users code true on visible, belonging community? it's ignoring condition user.profile.notification => true
user.joins(:codes => :community).where("codes.visible=? , communities.id=?", true, @community.id).count
Comments
Post a Comment