Last active
December 29, 2015 02:59
-
-
Save laspluviosillas/7604914 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
class Village < AR::Base | |
has_many :discussions | |
has_many :conversations, through: :discussions | |
has_many :changes, through: :conversations | |
def top_change | |
changes.merge Change.top_ranked(false) | |
end | |
end | |
class Discussion < AR::Base | |
belongs_to :village | |
has_many :conversations | |
end | |
class Conversation < AR::Base | |
belongs_to :discussion | |
belongs_to :change | |
end | |
class Change | |
has_one :conversation | |
def self.top_ranked(load_conversation=true) | |
with_rank(load_conversation).order("rank asc").limit(1) | |
end | |
def self.with_rank(load_conversation=true) | |
q = select("changes.*, SUM(conversations.rank) as rank") | |
q = q.joins(:conversation) if load_conversation | |
return q | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment