Last active
December 29, 2015 03:09
-
-
Save Jragon/7605340 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
2.0.0-p247 :004 > Change.all.each { |c| c.with_rank.rank } | |
Change Load (0.6ms) SELECT "changes".* FROM "changes" | |
NoMethodError: undefined method `with_rank' for #<Change:0x00000004272050> | |
from /home/jragon/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/attribute_methods.rb:436:in `method_missing' | |
from /home/jragon/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/attribute_methods.rb:131:in `method_missing' | |
from (irb):4:in `block in irb_binding' | |
from /home/jragon/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:13:in `each' | |
from /home/jragon/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:13:in `each' | |
from (irb):4 | |
from /home/jragon/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start' | |
from /home/jragon/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start' | |
from /home/jragon/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>' | |
from bin/rails:4:in `require' | |
from bin/rails:4:in `<main>' | |
2.0.0-p247 :005 > |
This file contains hidden or 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 Change < ActiveRecord::Base | |
has_many :conversations | |
validates :name, presence: true | |
# def self.top | |
# with_rank.order("rank desc").limit(1) | |
# end | |
# def self.with_rank | |
# joins(:conversations).select("changes.*, SUM(conversations.rank) as rank") | |
# end | |
def self.top_ranked(load_conversation=true) | |
with_rank(load_conversation).order("rank desc").limit(1) | |
end | |
def self.with_rank(load_conversation=true) | |
q = select("changes.*, SUM(conversations.rank) as rank") | |
q = q.joins(:conversations) if load_conversation | |
return q | |
end | |
end |
This file contains hidden or 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 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 |
This file contains hidden or 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
2.0.0-p247 :002 > Conversation.all | |
Conversation Load (0.8ms) SELECT "conversations".* FROM "conversations" | |
=> #<ActiveRecord::Relation [ | |
#<Conversation id: 1, discussion_id: 1, change_id: 2, rank: 1, ten_seed: 10, observations: "It helped\nA lot", created_at: "2013-11-21 19:28:09", updated_at: "2013-11-21 19:28:09">, | |
#<Conversation id: 2, discussion_id: 1, change_id: 1, rank: 2, ten_seed: 8, observations: "It helped quite a bit", created_at: "2013-11-21 19:29:56", updated_at: "2013-11-21 19:29:56">, | |
#<Conversation id: 3, discussion_id: 1, change_id: 4, rank: 3, ten_seed: 8, observations: "Wasn't the best", created_at: "2013-11-21 19:30:17", updated_at: "2013-11-21 21:34:51">]> | |
2.0.0-p247 :003 > Change.all | |
Change Load (0.3ms) SELECT "changes".* FROM "changes" | |
=> #<ActiveRecord::Relation [ | |
#<Change id: 1, name: "Health", created_at: "2013-11-21 19:19:44", updated_at: "2013-11-21 19:19:44">, | |
#<Change id: 2, name: "School", created_at: "2013-11-21 19:19:49", updated_at: "2013-11-21 19:19:49">, | |
#<Change id: 4, name: "Boarhole", created_at: "2013-11-21 19:20:28", updated_at: "2013-11-21 19:20:28">]> |
This file contains hidden or 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 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 |
This file contains hidden or 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
2.0.0-p247 :004 > Village.first.top_change.to_sql | |
Village Load (0.7ms) SELECT "villages".* FROM "villages" ORDER BY "villages"."id" ASC LIMIT 1 | |
=> "SELECT changes.*, SUM(conversations.rank) as rank FROM \"changes\" INNER JOIN \"conversations\" \"conversations_changes\" ON \"conversations_changes\".\"change_id\" = \"changes\".\"id\" INNER JOIN \"conversations\" ON \"changes\".\"id\" = \"conversations\".\"change_id\" INNER JOIN \"discussions\" ON \"conversations\".\"discussion_id\" = \"discussions\".\"id\" WHERE \"discussions\".\"village_id\" = ? ORDER BY rank desc LIMIT 1" |
This file contains hidden or 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 < 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 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment