named_scope not retrieving id

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

Any ideas?

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

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