Skip to content

Instantly share code, notes, and snippets.

@laspluviosillas
Last active December 29, 2015 02:59
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/7604914 to your computer and use it in GitHub Desktop.
Save laspluviosillas/7604914 to your computer and use it in GitHub Desktop.
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