#113 ✓hold
Chris Wanstrath

empty? on associations

Reported by Chris Wanstrath | September 18th, 2007 @ 12:41 PM

Nicolas wants:

class Provider < AR
 has_many :orders
end

class Order < AR
 belongs_to :provider
end

Provider.select { |p| p.name =~ 'A%' && p.orders.empty? }

(to retrieve all the providers which name begins by A and have no orders)

Mislav says:

SELECT ... FROM providers
LEFT JOIN orders ON provider.id = orders.provider_id
WHERE providers.name LIKE 'A%'
GROUP BY providers.id
HAVING COUNT(orders.id ) = 0

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Shared Ticket Bins

People watching this ticket

Tags

Pages