Why does this Rails with the name scope return empty (uninitialized?) Objects?

In a Rails application, I have a model Machinethat contains the following named scope:

named_scope :needs_updates, lambda {
  { :select => self.column_names.collect{|c| "\"machines\".\"#{c}\""}.join(','),
    :group => self.column_names.collect{|c| "\"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]
  }
}

This named area works great in design mode. However, in production mode, it returns 2 models as expected, but the models are empty or uninitialized; that is, the actual objects are returned (not nil), but all the fields nil. For example, checking the return value of a named area in the console returns the following:

[#<Machine >, #<Machine >]

But, as you can see, all fields of returned objects are set to nil.

The production and development environment is essentially the same. Both use the SQLite database. Here is the SQL statement generated for the query:

SELECT
  "machines"."id",
  "machines"."machine_name",
  "machines"."hostname",
  "machines"."mac_address",
  "machines"."ip_address",
  "machines"."hard_drive",
  "machines"."ram",
  "machines"."machine_type",
  "machines"."use",
  "machines"."comments",
  "machines"."in_use",
  "machines"."model",
  "machines"."vendor_id",
  "machines"."operating_system_id",
  "machines"."location",
  "machines"."acquisition_date",
  "machines"."rpi_tag",
  "machines"."processor",
  "machines"."processor_speed",
  "machines"."manual_updates",
  "machines"."serial_number",
  "machines"."owner"
FROM
  "machines"
LEFT JOIN
  "machine_updates" ON "machine_updates"."machine_id" = "machines"."id"
GROUP BY
  "machines"."id",
  "machines"."machine_name",
  "machines"."hostname",
  "machines"."mac_address",
  "machines"."ip_address",
  "machines"."hard_drive",
  "machines"."ram",
  "machines"."machine_type",
  "machines"."use",
  "machines"."comments",
  "machines"."in_use",
  "machines"."model",
  "machines"."vendor_id",
  "machines"."operating_system_id",
  "machines"."location",
  "machines"."acquisition_date",
  "machines"."rpi_tag",
  "machines"."processor",
  "machines"."processor_speed",
  "machines"."manual_updates",
  "machines"."serial_number",
  "machines"."owner"
HAVING
  "machines"."manual_updates" = 't'
  AND "machines"."in_use" = 't'
  AND (MAX("machine_updates"."date") IS NULL
       OR MAX("machine_updates"."date") < '2010-03-26 13:46:28')

, ?

+3
3

, , , : - -?

, , ( railscast # 115).

, ActiveRecords - AR .

, AR, "" , , .

, !

0

, . dev, ?

0

, , , .

[#<Machine >, #<Machine >]

, "" ... . , Machine, , , ?

Machine.needs_updates.each do |m|
  p m.inspect
end

?

-. , SQL mysql , , SQL... , .

0
source

Source: https://habr.com/ru/post/1739630/


All Articles