# Named\_scope not working

**URL:** <https://www.ruby-forum.com/t/named-scope-not-working/182431>\
**Category:** Rails\
**Created:** [January 27, 2010, 11:31pm UTC](https://www.ruby-forum.com/t/named-scope-not-working/182431 "2010-01-27T23:31:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 27, 2010, 11:31pm UTC](https://www.ruby-forum.com/t/named-scope-not-working/182431/1 "2010-01-27T23:31:52Z")

</div>

I am just trying to have the current\_user only view students from his or  
her site. I wrestled with this many times and others say this should  
work but it only returns an empty set:

StudentsController

def index\_scoper  
if current\_user.role\_id == 8  
if current\_user.contact and current\_user.contact.contactable  
students = current\_user.contact.contactable.students  
else  
students = []  
end  
else  
super.with\_state.with\_site  
end  
end

Students Model  
named\_scope :contactable, lambda { |current\_user|

```
case
when current_user.contact.contactable_id = 1 &&

```

current\_user.contact.contactable\_type = ‘site’ then  
{ :conditions =\> {:site\_id =\> 1}}  
when current\_user.contact.contactable\_id = 2 &&  
current\_user.contact.contactable\_type = ‘site’ then  
{ :conditions =\> {:site\_id =\> 2}}  
end

Model relationships:  
A contact has\_one user. A contact belongs\_to site via contactable\_id and  
contactable\_type (polymorphic relationship). A site has\_many students.

Any suggestion or clue as to why the named\_scope doesn’t show students  
with site\_id same as the current\_user?

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 28, 2010, 1:29am UTC](https://www.ruby-forum.com/t/named-scope-not-working/182431/2 "2010-01-28T01:29:51Z")

</div>

On Jan 27, 10:31 pm, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```

This looks odd - you’ve got single equals when you probable wanted to  
compare and not assign

Fred
