Question: How can I include a where clause on the `Reply` table with the following query?

```
Reply.joins(:replier_account).where(replier_accounts: {account_type: 'reply' })

```

I tried adding it at the end like so: 

```
Reply.joins(:replier_account).where(replier_accounts: {account_type: 'reply' }).where("created_at > ?", 7.days.ago)

```

But I'm receiving the following error:

```
ActiveRecord::StatementInvalid: PG::AmbiguousColumn: ERROR:  column reference "created_at" is ambiguous

```

Answer: 
The error tells you it doesn't know which table you want the 'created\_at' to be filtered on (At a guess, they both have 'created\_at'). Try affixing the table name to the created\_at; like so:

```
Reply.joins(:replier_account)
     .where(replier_accounts: {account_type: 'reply' })
     .where("replier_accounts.created_at > ?", 7.days.ago)

```
The other way to rewrite this query is to merge WHERE conditions into one call.

To fetch replies whose accounts were created in the last 7 days:

```
Reply.joins(:replier_account)
     .where(
        "replier_accounts.account_type = :type AND replier_accounts.created_at > :created_at",
        { type: 'reply', created_at: 7.days.ago }
     )

```

Or, to fetch replies which were created in last 7 days:

```
Reply.joins(:replier_account)
     .where(
        "replier_accounts.account_type = :type AND replies.created_at > :created_at",
        { type: 'reply', created_at: 7.days.ago }
     )

```