Skip to content

Instantly share code, notes, and snippets.

@adamancini
Created August 15, 2012 18:57
Show Gist options
  • Save adamancini/3362557 to your computer and use it in GitHub Desktop.
Save adamancini/3362557 to your computer and use it in GitHub Desktop.
class Event < ActiveRecord::Base
attr_accessible :base_station_id, :data, :data_type, :device_id
validates :base_station_id, :presence => true
validates :device_id, :presence => true
validates :data_type, :presence => true
belongs_to :base_station
belongs_to :device
def method_missing(id, *args, &block)
if data.has_key?(id.to_s)
data.fetch(id.to_s)
else
super
end
end
# Want to generate the following sql:
# pass Date object as date to function
# SELECT COUNT(*) FROM `events` WHERE (`events`.`created_at` BETWEEN <day-1> AND <day+1>)
def self.total_events_by_day(date)
self.count(:all, :conditions => {:created_at => date-1..date+1})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment