Skip to content

Instantly share code, notes, and snippets.

@dima4p
Created April 8, 2015 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dima4p/c1524556bbea46e3b863 to your computer and use it in GitHub Desktop.
Save dima4p/c1524556bbea46e3b863 to your computer and use it in GitHub Desktop.
# == Schema Information
#
# Table name: quota
#
# id :integer not null, primary key
# campaign_id :integer
# target :integer default("0")
# completed :integer default("0")
# parent_ids :text default("{}"), not null, is an Array
# settings :json default("{}")
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_quota_on_campaign_id (campaign_id)
# index_quota_on_completed (completed)
# index_quota_on_parent_ids (parent_ids)
# index_quota_on_target (target)
#
class Quotum < ActiveRecord::Base
belongs_to :campaign, inverse_of: :quota
has_one :client, through: :campaign
validates :campaign, presence: true
scope :roots, -> { where(parent_ids: '{}') }
class << self
def update_completed_stats(stats)
where(id: stats.keys).find_each do |quotum|
quotum.update_attributes(completed: stats[quotum.id.to_s])
end
end
handle_asynchronously :update_completed_stats
end
def children
self.class.where('? = ANY(parent_ids)', id.to_s)
end
def parents
self.class.where(id: parent_ids)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment