nktokyo
nktokyo

Reputation: 672

Order the result of a join

I have two tables:

Deal has many Opportunities
Opportunity belongs to Deal

I'm trying to order a join query that displays a list of opportunities based on their status and also the status of the deal. The query works fine except when I add .order("deal.addr_street desc") at the end.

@opportunities = Opportunity.joins(:opportunity_status).where("opportunity_statuses.id in (1, 2, 3, 6)").joins(:deal).merge(Deal.joins(:deal_status).where("deal_statuses.deal_status in ('On Market', 'Pre-sale')")).order("deal.addr_street desc")

The error is:

*SQLException: no such column: opportunity.deal.addr_street: SELECT "opportunities".* FROM "opportunities" INNER JOIN "opportunity_statuses" ON "opportunity_statuses"."id" = "opportunities"."opportunity_status_id" INNER JOIN "deals" ON "deals"."id" = "opportunities"."deal_id" LEFT OUTER JOIN "deal_statuses" ON "deal_statuses"."id" = "deals"."deal_status_id" WHERE (opportunity_statuses.id in (1, 2, 3, 6)) AND (deal_statuses.deal_status in ('On Market', 'Pre-sale')) ORDER BY opportunity.deal.addr_street desc*

Is anyone able to tell me how can I order the result of the join? I'm on rails4

Nick

Upvotes: 1

Views: 462

Answers (1)

Carlos Ramirez III
Carlos Ramirez III

Reputation: 7434

You need to pluralize your table name in the order clause

(...).order("deals.addr_street desc")

Upvotes: 1

Related Questions