Named scope returns uninitialized objects

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?

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:

[#<Machine >, #<Machine >]

(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?

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

Fred

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.