Skip to content

Instantly share code, notes, and snippets.

@dmichael
Last active December 11, 2015 11:08
Show Gist options
  • Select an option

  • Save dmichael/4591766 to your computer and use it in GitHub Desktop.

Select an option

Save dmichael/4591766 to your computer and use it in GitHub Desktop.
require 'squeel'
class RealtimeMessage < ActiveRecord::Base
# These attributes are "mass-assignable", that is, they come in directly from the serial port as XML
attr_accessible :src, :dsb, :time, :tmprF, :sensor_num, :sensor_type, :radio_id, :ch1_watts, :ch2_watts, :ch3_watts, :mac_address
def self.this_month
now = Time.now
first_of_month = Date.civil(now.year, now.month, 1)
last_of_month = Date.civil(now.year, now.month, -1)
self.where { # select all the records for this month (inclusive)
(created_at >= first_of_month) & (created_at <= last_of_month)
}
.where { # Filter out bad records (which happens)
(ch1_watts.not_eq nil) | (ch2_watts.not_eq nil) | (ch3_watts.not_eq nil)
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment