# Revising named\_scope semantics in associated classes

**URL:** <https://www.ruby-forum.com/t/revising-named-scope-semantics-in-associated-classes/151541>\
**Category:** Rails\
**Created:** [October 22, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/revising-named-scope-semantics-in-associated-classes/151541 "2008-10-22T17:37:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47c2d/32.png) [@sven](https://www.ruby-forum.com/u/sven)\
**Post date:** [October 22, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/revising-named-scope-semantics-in-associated-classes/151541/1 "2008-10-22T17:37:09Z")

</div>

Suppose I have the following models:

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

class Category \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :posts  
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:

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

```
def self.active_posts
  channels.active.all :conditions => {:show_in_category => true}
end

```

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:** ![sven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47c2d/32.png) [@sven](https://www.ruby-forum.com/u/sven)\
**Post date:** [October 22, 2008, 7:16pm UTC](https://www.ruby-forum.com/t/revising-named-scope-semantics-in-associated-classes/151541/2 "2008-10-22T19:16:29Z")

</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://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47c2d/32.png) [@sven](https://www.ruby-forum.com/u/sven)\
**Post date:** [October 22, 2008, 7:44pm UTC](https://www.ruby-forum.com/t/revising-named-scope-semantics-in-associated-classes/151541/3 "2008-10-22T19:44:01Z")

</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):

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

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

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

I want to be able to do this

some\_category.posts.active

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

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

```
def active_posts
  posts.active.all :conditions =>

```

[‘category\_assignment.show\_in\_category = ?’, true]  
end  
end

So when I call

Post.active

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

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:

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

And then remember to call

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:** ![sven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e47c2d/32.png) [@sven](https://www.ruby-forum.com/u/sven)\
**Post date:** [October 22, 2008, 8:12pm UTC](https://www.ruby-forum.com/t/revising-named-scope-semantics-in-associated-classes/151541/4 "2008-10-22T20:12:11Z")

</div>

On Wed, Oct 22, 2008 at 1:43 PM, Sven [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> …  
> 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.

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
