ruby on rails - ActiveRecord query based on belongs_to association count -


i have user model has may scores

class user < activerecord::base   has_many :scores 

i want able query postgresql db return users have @ least 1 score.

user.where(scores.count >= 1) 

any idea on how accomplish without adding counter_cache?

a more rails-ish way

user.joins(:scores).where('scores.user_id > 0') 

also, think select count(*) return number of users, not user rows themselves, i'm not sure that.


Comments

Popular posts from this blog

How to logout from a login page in asp.net -

Stack level too deep error after upgrade to rails 3.2 and ruby 1.9.3 -