Skip to content

Instantly share code, notes, and snippets.

@prpetten
Created October 22, 2013 02:05
Show Gist options
  • Save prpetten/7094148 to your computer and use it in GitHub Desktop.
Save prpetten/7094148 to your computer and use it in GitHub Desktop.
Using the new hash option for ActiveRecord order clause will cause a loss of context of the original table when merging the scope from another table.
unless File.exists?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts do |t|
t.string :subject
end
create_table :comments do |t|
t.integer :post_id
t.integer :points
end
end
class Post < ActiveRecord::Base
has_many :comments
scope :dewey, -> { order(subject: :desc) }
scope :truman, -> { order(:subject) }
end
class Comment < ActiveRecord::Base
belongs_to :post
scope :decimal, -> { order(points: :desc) }
scope :dewey_decimal, -> { decimal.joins(:post).merge(Post.dewey) }
scope :truman_decimal, -> { decimal.joins(:post).merge(Post.truman) }
end
class BugTest < Minitest::Test
def test_association_stuff
post1 = Post.create!(subject: "Awesomeness")
comment1 = Comment.create!(post_id: post1.id, points: 5)
comment2 = Comment.create!(post_id: post1.id, points: 9)
post2 = Post.create!(subject: "Zaniness")
comment3 = Comment.create!(post_id: post2.id, points: 9)
comment4 = Comment.create!(post_id: post2.id, points: 3)
assert_equal Comment.truman_decimal, [comment2, comment3, comment1, comment4]
assert_equal Comment.dewey_decimal, [comment3, comment2, comment1, comment4]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment