How do I group and order by a model without loosing includes() method benefits

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How do I group and order by a model without loosing includes() method benefits

Arup Rakshit
I have a Project and Bid model.

Project has_many :bids
Bid belongs_to project

I have now
 relation = Project.includes(:bids, :user).references(:users, :bids)

What I now want is : select projects by ordering them with their bids count. How can I write this query?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAKxHnE1VBowD-dGZTFBgFBDVrznRj29HVBYprSphyJzxtGdD0w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

How do I group and order by a model without loosing includes() method benefits

Sampson Crowley
relation = Project.
  joins(
    <<-SQL
      LEFT JOIN (
        SELECT
          bids.project_id,
          COUNT(bids.id) AS bid_count
        FROM bids
        GROUP BY bids.project_id
      ) bid_counts
        ON bid_counts.project_id = projects.id
    SQL
  ).
  order('bid_counts.bid_count').
  includes(:bids, :user).
  references(:users, :bids)

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0e187dcc-0285-4516-afef-1db14fb4f5e0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I group and order by a model without loosing includes() method benefits

McNeal Maddox
In reply to this post by Arup Rakshit
Have you tried adding a 'where' condition to your query?

E.G., relation = Project.includes(:bids, :user).references(:users, :bids).where(bids > 0).sort.reverse

On Thu, Jun 6, 2019 at 4:06 AM Arup Rakshit <[hidden email]> wrote:
I have a Project and Bid model.

Project has_many :bids
Bid belongs_to project

I have now
 relation = Project.includes(:bids, :user).references(:users, :bids)

What I now want is : select projects by ordering them with their bids count. How can I write this query?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAKxHnE1VBowD-dGZTFBgFBDVrznRj29HVBYprSphyJzxtGdD0w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAKPjS73yPSgEnp0sq%3DJZsYNCMGd6GPqtGV6qbv30uWhi9rLFGA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How do I group and order by a model without loosing includes() method benefits

Rob Biedenharn-3
In reply to this post by Arup Rakshit
This probably works:

relation = Project.joins(:bids).group(Arel.sql('projects.id')).order(Arel.sql('count(bids.id) DESC'))

However, you might have to play around with it a bit if you need to put the `.includes(:bids, :user)` back in (or take the 1+N hit)

-Rob


On 2019-Jun-6, at 07:06 , Arup Rakshit <[hidden email]> wrote:

I have a Project and Bid model.

Project has_many :bids
Bid belongs_to project

I have now
 relation = Project.includes(:bids, :user).references(:users, :bids)

What I now want is : select projects by ordering them with their bids count. How can I write this query?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAKxHnE1VBowD-dGZTFBgFBDVrznRj29HVBYprSphyJzxtGdD0w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/134FF291-A6DB-4825-AD1B-A153511FBB07%40gmail.com.
For more options, visit https://groups.google.com/d/optout.