Crusader633
Crusader633

Reputation: 595

using a subquery in rails

I have the following statement:

@addresses = Location.joins(:Address).where('address.address_id = ?', Location.all.select('address_id').uniq.where(['delivery_id = ?', @delivery.id])).order('time')

It doesn`t work, it prints an error near in my first where statement.

PG::SyntaxError: FEHLER:  Syntaxfehler bei "SELECT"
LINE 1: ... = "locations"."id" WHERE (addresses.address_id = SELECT DIS...
                                                         ^
: SELECT "locations".* FROM "locations" INNER JOIN "addresses" ON "addresses"."l
ocation_id" = "locations"."id" WHERE (addresses.address_id = SELECT DISTINCT add
ress_id FROM "locations"  WHERE (delivery_id = 6))  ORDER BY time
Completed 500 Internal Server Error in 4ms

Upvotes: 0

Views: 116

Answers (1)

Brian Glick
Brian Glick

Reputation: 2201

Assuming that addesses.address_id is the correct field name, you also need to use IN instead of =. = expects one result, but you're returning multiple results which requires IN.

where('address.address_id IN (?)',...

Upvotes: 1

Related Questions