Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save everton/7eed6c00c42712cc0a1f to your computer and use it in GitHub Desktop.
Save everton/7eed6c00c42712cc0a1f to your computer and use it in GitHub Desktop.
blongs_to unscope not respected in includes - rails 4.2.3
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.2.3'
gem 'sqlite3'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# 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, force: true do |t|
t.string :title
t.datetime :deleted_at
end
create_table :comments, force: true do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_many :comments
default_scope { where deleted_at: nil }
end
class Comment < ActiveRecord::Base
belongs_to :post, -> { unscope(where: :deleted_at) }
scope :sorted_post_title, -> {
includes(:post).order('posts.title ASC')
}
def post_title
post.title
end
end
class BugTest < Minitest::Test
def test_association_stuff
post1 = Post.create! title: 'Post 1'
post2 = Post.create! title: 'Post 2'
comment1 = Comment.create! post: post1
comment2 = Comment.create! post: post2
post1.deleted_at = DateTime.now
post1.save!
assert_equal 1, Post.count
assert_equal ['Post 1', 'Post 2'], Comment.sorted_post_title.map(&:post_title)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment