# Named scope returns uninitialized objects

**URL:** <https://www.ruby-forum.com/t/named-scope-returns-uninitialized-objects/187021>\
**Category:** Rails\
**Created:** [April 15, 2010, 10:08pm UTC](https://www.ruby-forum.com/t/named-scope-returns-uninitialized-objects/187021 "2010-04-15T22:08:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdeering](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@mdeering](https://www.ruby-forum.com/u/mdeering)\
**Post date:** [April 15, 2010, 10:08pm UTC](https://www.ruby-forum.com/t/named-scope-returns-uninitialized-objects/187021/1 "2010-04-15T22:08:44Z")

</div>

Hello,

An issue with a Rails named scope has been stumping me for quite  
sometime, and I’m hoping someone can point out the flaw. I have a model,  
Machine, with the following named scope:

named\_scope :needs\_updates, lambda {  
{ :select =\> self.column\_names.collect{|c|  
%Q/“machines”."#{c}"/}.join(’,’),  
:group =\> self.column\_names.collect{|c|  
%Q/“machines”."#{c}"/}.join(’,’),  
:joins =\> ‘LEFT JOIN “machine\_updates” ON  
“machine\_updates”.“machine\_id” = “machines”.“id”’,  
:having =\> [’“machines”.“manual\_updates” = ? AND  
“machines”.“in\_use” = ? AND (MAX(“machine\_updates”.“date”) IS NULL OR  
MAX(“machine\_updates”.“date”) \< ?)’,  
true, true, UPDATE\_THRESHOLD.days.ago]  
}  
}

The named scope works fine in development mode, and with my current  
dataset returns two models as expected. In production mode, however, it  
returns two models, but they are uninitialized; that is, actual objects  
are returned, but all the fields (including id) are nil. When inspecting  
the return value of the named scope in the console, the following is  
returned:

[#, #]

(That is, objects with no values.)

The production and development databases are essentially the sameâ€”both  
are using SQLite for DB storage. I’m using Rails 2.3.5 with no plugins.

Any idea what’s wrong?

* * *

Michael D.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mdeering](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@mdeering](https://www.ruby-forum.com/u/mdeering)\
**Post date:** [April 15, 2010, 11:51pm UTC](https://www.ruby-forum.com/t/named-scope-returns-uninitialized-objects/187021/2 "2010-04-15T23:51:12Z")

</div>

> Have you tried running the sql this generates by hand and seeing if  
> the result set looks normal ?

Yep. Running the SQL generated by Rails (via script/dbconsole) returns  
the expected result set.

* * *

Michael D.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mdeering](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@mdeering](https://www.ruby-forum.com/u/mdeering)\
**Post date:** [April 15, 2010, 10:10pm UTC](https://www.ruby-forum.com/t/named-scope-returns-uninitialized-objects/187021/3 "2010-04-15T22:10:30Z")

</div>

On Apr 15, 6:46 pm, Michael D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> }
> 
> ```
> 
> Any idea what’s wrong?  
> Have you tried running the sql this generates by hand and seeing if  
> the result set looks normal ?

Fred
