Display Users without Posts in Rails -


how query users have yet write posts? trying:

u = user.joins(:posts).where('posts.size = ?', 0) 

output:

pg::undefinedcolumn: error:  column posts.size not exist 

trying wrap head around correct sql syntax, no luck far

you must without join, this:

 user.where(post.where("posts.user_id = users.id").exists.not) 

its useful chain conditions (like applying different filters).


creates sql like:

select "users".* "users"   (   not (     exists (select "posts".* "posts"  (posts.user_id = users.id))   ) ) 

Comments

Popular posts from this blog

android - How to save instance state of selected radiobutton on menu -

python 3 IndexError: list index out of range -

IF statement in MySQL trigger -