T
T
Tesla_Rails2016-02-21 12:13:20
Ruby on Rails
Tesla_Rails, 2016-02-21 12:13:20

What is the best way to sort posts in Rails?

How can a scope be written to sort users who have created posts?
user has_many :posts
post belongs_to :user

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Demidenko, 2016-02-21
@Tesla_Rails

scope :with_posts, -> { where('EXISTS (SELECT 1 FROM posts WHERE users.id = posts.user_id)') }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question