Zack
Zack

Reputation: 2497

Activequery where clause with a join

I have two tables, Notes and Accounts. A note can have many accounts. I am attempting to get all notes of a certain type that were created after January 1, 2015. I would also like to load the accounts at the same time using the .joins function of ActiveQuery.

This is what I am attempting:

notes = Note.joins(:account).where(type: 'red').where('created_at > ?', '2015-01-01')

This should give me an array of all Notes of Type 'red' that were created after the first of the year. The issue is that created_at is ambiguous. How do I specify that I want it to refer to notes.created_at?

Upvotes: 0

Views: 38

Answers (1)

markets
markets

Reputation: 7033

Just specify the table name on where clause:

Note.joins(:account).where(type: 'red').where('notes.created_at > ?', '2015-01-01')

Upvotes: 2

Related Questions