Skip to content

Instantly share code, notes, and snippets.

@laspluviosillas
Forked from Jragon/Change.all.each { |c| c.with_rank.rank }
Last active December 29, 2015 03:09
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 laspluviosillas/7605863 to your computer and use it in GitHub Desktop.
Save laspluviosillas/7605863 to your computer and use it in GitHub Desktop.
class Change < ActiveRecord::Base
has_many :conversations
validates :name, presence: true
def self.top_ranked(load_conversation=true)
with_rank(load_conversation).order("conversations_rank desc").limit(1)
end
def self.with_rank(load_conversation=true)
q = select("changes.*, SUM(conversations.rank) as conversations_rank").group("changes.id")
q = q.joins(:conversations) if load_conversation
return q
end
end
class Conversation < ActiveRecord::Base
belongs_to :discussion
belongs_to :change
validates :discussion_id, :change_id, :rank, :ten_seed, presence: true
validates :change_id, :rank, uniqueness: { scope: :dicussion_id }
end
class Discussion < ActiveRecord::Base
belongs_to :village
belongs_to :group
has_many :conversations
validates :lead_facilitator, :duration, :date_held,
:group_id, :village_id, presence: true
end
class Village < ActiveRecord::Base
belongs_to :programme
has_many :discussions
has_many :conversations, through: :discussions
has_many :changes, through: :conversations
validates :name, :programme_id, presence: true
def top_change
changes.merge Change.top_ranked(false)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment