Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unless File.exist?('Gemfile')
- File.write('Gemfile', <<-GEMFILE)
- source 'https://rubygems.org'
- gem 'rails', github: 'rails/rails'
- gem 'arel', github: 'rails/arel'
- 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.integer :parent_post_id, :integer
- t.integer :tree_state_updated_at, :datetime
- t.timestamps
- end
- end
- class Post < ActiveRecord::Base
- belongs_to :parent_post,
- touch: :tree_state_updated_at,
- foreign_key: :parent_post_id,
- class_name: self.name,
- inverse_of: :children_posts
- has_many :children_posts,
- dependent: :nullify,
- foreign_key: :parent_post_id,
- class_name: self.name,
- inverse_of: :parent_post
- class << self
- def created_recently
- where(created_at: 1.day.ago..Time.now)
- end
- def children_only
- where.not(parent_post_id: nil)
- end
- def with_children_only
- where(id: Post.children_only.select(:parent_post_id))
- end
- end
- end
- class BugTest < Minitest::Test
- def test_orders_of_scopes_does_not_affect_sql
- parent_post = Post.create!
- old_child_post = Post.create!(parent_post: parent_post, created_at: 1.month.ago)
- # puts Post.created_recently.with_children_only.to_sql
- # puts Post.with_children_only.created_recently.to_sql
- assert Post.created_recently.with_children_only.include?(parent_post)
- assert Post.with_children_only.created_recently.include?(parent_post)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement