# named\_scope - :group (finds first entry?)

**URL:** <https://discuss.rubyonrails.org/t/named-scope-group-finds-first-entry/54726>\
**Category:** rubyonrails-core\
**Created:** [September 29, 2010, 2:03pm UTC](https://discuss.rubyonrails.org/t/named-scope-group-finds-first-entry/54726 "2010-09-29T14:03:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pete6](https://avatars.discourse-cdn.com/v4/letter/p/d26b3c/32.png) [@Pete6](https://discuss.rubyonrails.org/u/Pete6)\
**Post date:** [September 29, 2010, 2:03pm UTC](https://discuss.rubyonrails.org/t/named-scope-group-finds-first-entry/54726/1 "2010-09-29T14:03:20Z")

</div>

Hi,

I am currently a little confused with trying to manipulate a named scope that I currently have.

I have two associated models e.g

Catalogue,

has\_many :articles

Article:

belongs\_to :catalogue

my named scope looks like this (in Class Article)

named scope :finished\_articles, { :conditions =\> [:finished =\> true] }

This works fine on it's own, but I need to trim it to show only the most recent finished article per catalogue (based on created\_at field) - currently I may have many finished articles per catalogue, but want to find the latest one.

I have tried to use two named scopes, the other looking like this:

named scope :limited, { :group =\> "catalogue\_id" }

which does only leave one entry per catalogue, but rather than being the most recent entry it is the first created. But I want to gather the most recently created article - one from each catalogue.

I just want to be able to reverse this order, but I can't seem to figure out how to do this (I have a default order already set, but this only seems to affect the scope after it has been rendered..) Also setting the order does not seem to have an effect on how the instances are gathered from the database.

Thank you for any suggestions!

---

<div class="post-metadata">

**Author:** ![Thiago\_Cifani](https://avatars.discourse-cdn.com/v4/letter/t/c0e974/32.png) [@Thiago\_Cifani](https://discuss.rubyonrails.org/u/Thiago_Cifani)\
**Post date:** [September 29, 2010, 6:04pm UTC](https://discuss.rubyonrails.org/t/named-scope-group-finds-first-entry/54726/2 "2010-09-29T18:04:52Z")

</div>

it it is rails 3 I think you could use only,

[http://www.railway.at/2010/03/09/named-scopes-are-dead/](http://www.railway.at/2010/03/09/named-scopes-are-dead/)

and

Active record querying ??

[http://edgeguides.rubyonrails.org/active\_record\_querying.html](http://edgeguides.rubyonrails.org/active_record_querying.html)

---

<div class="post-metadata">

**Author:** ![radar](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/radar/32/604_2.png) [@radar](https://discuss.rubyonrails.org/u/radar)\
**Post date:** [September 29, 2010, 9:10pm UTC](https://discuss.rubyonrails.org/t/named-scope-group-finds-first-entry/54726/3 "2010-09-29T21:10:53Z")

</div>

Hi Pete,

This list is for discussions about the core of Ruby on Rails, such as patches and bug fixes.

For general help please ask your question on the rubyonrails-talk mailing list.
