# Named\_scope not retrieving id

**URL:** <https://www.ruby-forum.com/t/named-scope-not-retrieving-id/196813>\
**Category:** Rails\
**Created:** [September 19, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/named-scope-not-retrieving-id/196813 "2008-09-19T16:30:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [September 19, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/named-scope-not-retrieving-id/196813/1 "2008-09-19T16:30:57Z")

</div>

Hi all,

I was having a problem with a named\_scope and wondered if anyone else  
had the same problem before and if there was a way to keep the  
named\_scope and work around this. I had the following code in a  
Student model:

named\_scope :preceptorless, :conditions =\>  
[“preceptor\_students.preceptor\_id IS NULL”], :joins =\> “LEFT OUTER  
JOIN preceptor\_students ON preceptor\_students.student\_id =  
students.id” }

I then referred to it without additional arguments. It would  
successfully return the records, but without an ID for each. I then  
changed it to the following code:

def self.preceptorless(\*args)  
with\_scope(:find =\>  
{ :conditions =\> [ “preceptor\_students.preceptor\_id IS  
NULL” ], :joins =\> “LEFT OUTER JOIN preceptor\_students ON  
preceptor\_students.student\_id = students.id” }) do  
find(\*args)  
end  
end

I then referred to it as Student.preceptorless(:all). It would then  
return the same records, but WITH an ID for the student.

Is there some documentation I am missing as to why the named\_scope did  
not return the ID of each student with the rest of the record? I’d  
love to keep this as a named\_scope since it reads a bit better in my  
opinion, but I need the IDs to populate javascript code in a view. I  
even tried on each specifying the :select and it still would return  
nil for the ID.

To clarify the models, I have a Preceptor model, a Student model, and  
a many-to-many relationship with extra data named PreceptorStudent.

If anyone can shed some light on this, I’d greatly appreciate it. I’m  
going to continue to look into why this was the case, but at the  
moment, I am at a loss.

Thanks for your views and help,  
Dave

---

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [September 22, 2008, 5:56pm UTC](https://www.ruby-forum.com/t/named-scope-not-retrieving-id/196813/2 "2008-09-22T17:56:26Z")

</div>

Any ideas?

---

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [September 22, 2008, 5:58pm UTC](https://www.ruby-forum.com/t/named-scope-not-retrieving-id/196813/3 "2008-09-22T17:58:47Z")

</div>

On 19 Sep 2008, at 15:10, Dave wrote:

> Hi all,
> 
> I was having a problem with a named\_scope and wondered if anyone else  
> had the same problem before and if there was a way to keep the  
> named\_scope and work around this. I had the following code in a  
> Student model:

you should add a :select or else the id from the table you join could  
squash the original id. You said you tried adding a :select clause -  
what was it ?

Fred

---

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [September 29, 2008, 7:34pm UTC](https://www.ruby-forum.com/t/named-scope-not-retrieving-id/196813/4 "2008-09-29T19:34:22Z")

</div>

Hi Fred,

I included things like the id and the first and last name. I also was  
specific on the table to pull from (table.field). Even when I  
specifically selected the id, it turned up nil, so I’m not sure.

Dave

On Sep 22, 11:57 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
