# Asserting scope in a model method

**URL:** <https://www.ruby-forum.com/t/asserting-scope-in-a-model-method/105818>\
**Category:** Rails\
**Created:** [June 3, 2007, 9:57am UTC](https://www.ruby-forum.com/t/asserting-scope-in-a-model-method/105818 "2007-06-03T09:57:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [June 3, 2007, 9:57am UTC](https://www.ruby-forum.com/t/asserting-scope-in-a-model-method/105818/1 "2007-06-03T09:57:25Z")

</div>

# lets say this is a comments model, acts\_as\_tree

# used like this @post.comments.find\_roots

def self.find\_roots(options = {})  
with\_scope :find =\> options do  
self.find(:conditions =\> [‘parent\_id IS NULL’], :order  
=\> :position )  
end  
end

# Question: how can i enforce any call is scoped by project\_id?

# (I dont want to allow Post.find\_roots, it must have a project\_id)

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [June 3, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/asserting-scope-in-a-model-method/105818/2 "2007-06-03T19:39:44Z")

</div>

Jonathan L. wrote:

> # lets say this is a comments model, acts\_as\_tree
> 
> # used like this @post.comments.find\_roots
> 
> def self.find\_roots(options = {})  
> with\_scope :find =\> options do  
> self.find(:conditions =\> [‘parent\_id IS NULL’], :order  
> =\> :position )  
> end  
> end
> 
> # Question: how can i enforce any call is scoped by project\_id?
> 
> # (I dont want to allow Post.find\_roots, it must have a project\_id)

You can define it on your association, if you only want that method  
accessible via the association.

class Post \< ActiveRecord::Base  
has\_many :comments do  
def find\_roots(options = {})  
#…  
end  
end  
end

Now you can do @post.comments.find\_roots but not Post.find\_roots
