Created
April 8, 2015 11:05
-
-
Save dima4p/c1524556bbea46e3b863 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == 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