# Revising named\_scope semantics in associated classes

**URL:** <https://discuss.rubyonrails.org/t/revising-named-scope-semantics-in-associated-classes/35626>\
**Category:** rubyonrails-talk\
**Created:** [October 22, 2008, 3:36pm UTC](https://discuss.rubyonrails.org/t/revising-named-scope-semantics-in-associated-classes/35626 "2008-10-22T15:36:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sven](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@Sven](https://discuss.rubyonrails.org/u/Sven)\
**Post date:** [October 22, 2008, 3:36pm UTC](https://discuss.rubyonrails.org/t/revising-named-scope-semantics-in-associated-classes/35626/1 "2008-10-22T15:36:21Z")

</div>

Suppose I have the following models:

&nbsp;&nbsp;class Post \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;&nbsp;has\_and\_belongs\_to\_many :categories &nbsp;&nbsp;&nbsp;&nbsp;named\_scope :active, :conditions =\> {:active =\> true} &nbsp;&nbsp;end

&nbsp;&nbsp;class Category \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;&nbsp;has\_and\_belongs\_to\_many :posts &nbsp;&nbsp;end

I can call Post.active to get the set of all active Post objects. And I can call some\_category.posts.active to get all active Post objects associated with some\_category.

But what if I want slightly different semantics for the active posts within a category? Perhaps there is an additional condition that should apply in this case, as suggested by the new method shown here:

&nbsp;&nbsp;class Category \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;&nbsp;has\_and\_belongs\_to\_many :posts

&nbsp;&nbsp;&nbsp;&nbsp;def self.active\_posts &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;channels.active.all :conditions =\> {:show\_in\_category =\> true} &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

Instead of introducing a new active\_posts method, is there any way to define conditions that should be merged into the named\_scope when it is reference from the associated class?

-Sven

---

<div class="post-metadata">

**Author:** ![Craig\_Demyanovich](https://avatars.discourse-cdn.com/v4/letter/c/e47c2d/32.png) [@Craig\_Demyanovich](https://discuss.rubyonrails.org/u/Craig_Demyanovich)\
**Post date:** [October 22, 2008, 5:15pm UTC](https://discuss.rubyonrails.org/t/revising-named-scope-semantics-in-associated-classes/35626/2 "2008-10-22T17:15:48Z")

</div>

I’m a little confused by your example. Do you mean posts instead of channels? :show\_in\_category is on Post, correct?

Assuming posts and :show\_in\_category on Post, couldn’t you just add a second named\_scope to Post, show\_in\_category (for lack of a better name)?

class Post \< ActiveRecord::Base named\_scope :active, :conditions =\> { :active =\> true } named\_scope :show\_in\_category, :conditions =\> { :show\_in\_category =\> true } … end

Then you could do some\_category.posts.active or some\_category.posts.active.show\_in\_category as you see fit.

Thoughts?

Regards, Craig

---

<div class="post-metadata">

**Author:** ![Sven](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@Sven](https://discuss.rubyonrails.org/u/Sven)\
**Post date:** [October 22, 2008, 5:43pm UTC](https://discuss.rubyonrails.org/t/revising-named-scope-semantics-in-associated-classes/35626/3 "2008-10-22T17:43:24Z")

</div>

Sorry, I made some mistakes when I tried to simplify my project code. I'm trying to amend the semantics of an existing named\_scope when it's called from an association, not add an additional one. Here's a more complete example (which I hope I've gotten right):

&nbsp;&nbsp;class Post \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;&nbsp;has\_many :category\_assignments &nbsp;&nbsp;&nbsp;&nbsp;has\_many :categories, :through =\> :category\_assignments &nbsp;&nbsp;&nbsp;&nbsp;named\_scope :active, :conditions =\> {:active =\> true} &nbsp;&nbsp;end

&nbsp;&nbsp;class Category \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;&nbsp;has\_many :category\_assignments &nbsp;&nbsp;&nbsp;&nbsp;has\_many :posts, :through =\> :category\_assignments &nbsp;&nbsp;end

&nbsp;&nbsp;class CategoryAssignment \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;&nbsp;belongs\_to :post &nbsp;&nbsp;&nbsp;&nbsp;belongs\_to :category &nbsp;&nbsp;&nbsp;&nbsp;# has attribute show\_in\_category which allows &nbsp;&nbsp;&nbsp;&nbsp;# showing/hiding posts on a per-category basis &nbsp;&nbsp;end

I want to be able to do this

&nbsp;&nbsp;some\_category.posts.active

And get the semantics of the active\_posts method below:

&nbsp;&nbsp;class Category \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;&nbsp;has\_many :category\_assignments &nbsp;&nbsp;&nbsp;&nbsp;has\_many :posts, :through =\> :category\_assignment

&nbsp;&nbsp;&nbsp;&nbsp;def active\_posts &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;posts.active.all :conditions =\> ['category\_assignment.show\_in\_category = ?', true] &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

So when I call

&nbsp;&nbsp;Post.active

I get the set of all active posts. But when I call

&nbsp;&nbsp;some\_category.posts.active

I get the set of all active posts in some\_category where show\_in\_category is true (like calling my active\_posts method, but using a named\_scope instead).

One possibility is to do something like this:

&nbsp;&nbsp;class Category \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;&nbsp;has\_many :category\_assignments &nbsp;&nbsp;&nbsp;&nbsp;has\_many :posts, :through =\> :category\_assignment &nbsp;&nbsp;&nbsp;&nbsp;has\_many :visible\_posts, :class\_name =\> 'Post', :through =\> :category\_assignment, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:conditions =\> ['category\_assignment.show\_in\_category = ?', true] &nbsp;&nbsp;end

And then remember to call

&nbsp;&nbsp;some\_category.visible\_posts.active

This lets me use the named scope instead of a method but the semantics aren't right. I'm trying to express the notion that active category.posts are subject to an additional restriction relative to active posts. I'm not trying to divide \_posts\_ into two groups. I'm just trying to restrict \_active\_ posts within a category.

-Sven

---

<div class="post-metadata">

**Author:** ![Craig\_Demyanovich](https://avatars.discourse-cdn.com/v4/letter/c/e47c2d/32.png) [@Craig\_Demyanovich](https://discuss.rubyonrails.org/u/Craig_Demyanovich)\
**Post date:** [October 22, 2008, 6:11pm UTC](https://discuss.rubyonrails.org/t/revising-named-scope-semantics-in-associated-classes/35626/4 "2008-10-22T18:11:21Z")

</div>

Restricting is dividing your set into those items you want and those you don’t. Since you have attributes across two models, Post#active and CategoryAssignment#show\_in\_category, though, I don’t think you can combine them into one named\_scope. I think you’ll have to do something like you suggested at the end of your last message, or you’ll have to wrap everything in a class method to have the semantics that you want.

Regards, Craig
