mdegis
mdegis

Reputation: 2528

Rails where condition with nested associations

Let's say I have Delivery model which has one Postman and that Postmanhas many Teams.

I would like to get all deliveries with specific team ID = 1 for example.

I have try:

Delivery.joins(:postman).where(:postmans => {team_ids: 1})

But I couldn't manage to get them. How can I achieve this?

Upvotes: 3

Views: 2945

Answers (1)

Pavan
Pavan

Reputation: 33542

You should do like below

Delivery.joins(:postman => :teams).where(:postman => {teams: {id: 1}})

Upvotes: 5

Related Questions