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 :001 > Village.first.top_change | |
Village Load (0.2ms) SELECT "villages".* FROM "villages" ORDER BY "villages"."id" ASC LIMIT 1 | |
(0.2ms) SELECT MAX(rank) AS max_id FROM "changes" INNER JOIN "conversations" ON "conversations"."change_id" = "changes"."id" | |
NoMethodError: undefined method `default_scoped?' for 3:Fixnum | |
from /home/jragon/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/relation/merger.rb:45:in `initialize' | |
from /home/jragon/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/relation/spawn_methods.rb:44:in `new' | |
from /home/jragon/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/relation/spawn_methods.rb:44:in `merge!' | |
from /home/jragon/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/relation/spawn_methods.rb:33:in `merge' | |
from /home/jragon/Code/Rails/OTIS/app/models/village.rb:10:in `top_change' | |
from (irb):1 | |
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>' |
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 & Change.top | |
end | |
end | |
class Change < ActiveRecord::Base | |
has_many :conversations | |
validates :name, presence: true | |
def self.top | |
with_rank.maximum("rank") | |
end | |
def self.with_rank | |
joins(:conversations).select("changes.*, SUM(conversations.rank) as rank") | |
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 | |
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment