Skip to content

Instantly share code, notes, and snippets.

@Jragon
Last active December 29, 2015 03:09
Show Gist options
  • Save Jragon/7605340 to your computer and use it in GitHub Desktop.
Save Jragon/7605340 to your computer and use it in GitHub Desktop.
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>'
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment