# Q: find\_by\_x\_id and named scopes

**URL:** <https://www.ruby-forum.com/t/q-find-by-x-id-and-named-scopes/161012>\
**Category:** Rails\
**Created:** [February 22, 2009, 10:44am UTC](https://www.ruby-forum.com/t/q-find-by-x-id-and-named-scopes/161012 "2009-02-22T10:44:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 22, 2009, 10:44am UTC](https://www.ruby-forum.com/t/q-find-by-x-id-and-named-scopes/161012/1 "2009-02-22T10:44:52Z")

</div>

Hi

I have a model Topic that has many Posts, and I’m trying to use a  
named scope on my find

I can do something like this:

@posts = @topic.posts.named\_scope\_func

But I cannot do this:

@posts = Post.find\_by\_topic\_id(1).named\_scope\_func

The reason is that the “posts” and “find\_by\_topic\_id” seem to return  
different classes (Post and Array)

Am I doing something wrong?

thanks

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 22, 2009, 12:12pm UTC](https://www.ruby-forum.com/t/q-find-by-x-id-and-named-scopes/161012/2 "2009-02-22T12:12:07Z")

</div>

Hey,

Named scopes generate a Class method. Looks like you’re trying to call  
one on an instance of Post (or an array with one Post in it)

You could try specifying the topic first and then calling  
posts.named\_scope

i.e.

@topic = Topic.find(1).posts.named\_scope

Don’t know any other way around this using named scopes I’m afraid

On Feb 22, 9:43 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 22, 2009, 12:15pm UTC](https://www.ruby-forum.com/t/q-find-by-x-id-and-named-scopes/161012/3 "2009-02-22T12:15:03Z")

</div>

Hey,  
Named scopes generate a Class method. Looks like you’re trying to  
call  
one on an instance of Post (or an array with one Post in it).

Don’t know any other way around this other than calling the class  
method on Topic.posts.named\_scope

Maybe @posts = Post.named\_scope.all :conditions =\> {:topic\_id =\> 1}

On Feb 22, 9:43 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
