# Named scope and has\_many

**URL:** <https://www.ruby-forum.com/t/named-scope-and-has-many/190641>\
**Category:** Rails\
**Created:** [June 24, 2010, 12:25pm UTC](https://www.ruby-forum.com/t/named-scope-and-has-many/190641 "2010-06-24T12:25:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [June 24, 2010, 12:25pm UTC](https://www.ruby-forum.com/t/named-scope-and-has-many/190641/1 "2010-06-24T12:25:54Z")

</div>

Hi  
I have the models User and Payment. The relation between them

user has\_many payments  
payment belongs\_to user

```
 payments table has a field paid_date. Now for finding out the last

```

paid\_date for a particular user I did like

class payment \< ActiveRecord::Base  
named\_scope :last\_payment\_date, :select =\> ‘MAX(date\_paid) as  
date\_paid’  
end

Then @user = User.find(id)  
@user.payments.last\_payment\_date

```
  I get the correct answer. But actually is this correct? Because

```

sql generated is

SELECT MAX(date\_paid) as date\_paid FROM `payments` WHERE  
(`payments`.user\_id = 8) AND (`payments`.user\_id = 8)

```
 I would like to know why (`payments`.user_id = 8) appears twice

```

Thanks
