Skip to content

Instantly share code, notes, and snippets.

@phoet
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phoet/158d163cf3a2428a8901 to your computer and use it in GitHub Desktop.
Save phoet/158d163cf3a2428a8901 to your computer and use it in GitHub Desktop.
double default_scope query
# A sample Gemfile
source "https://rubygems.org"
gem 'activerecord', github: 'rails/rails', branch: 'master'
gem 'arel', github: 'rails/arel', branch: 'master'
gem 'pg'
GIT
remote: git://github.com/rails/arel.git
revision: d69146d3db96116ef241e3c8b0eb122104bb780b
branch: master
specs:
arel (6.0.0.beta2)
GIT
remote: git://github.com/rails/rails.git
revision: 4426b726bfd3f7d26224c2d8ffdc3c00cd1b561a
branch: master
specs:
activemodel (4.2.0.beta4)
activesupport (= 4.2.0.beta4)
builder (~> 3.1)
activerecord (4.2.0.beta4)
activemodel (= 4.2.0.beta4)
activesupport (= 4.2.0.beta4)
arel (>= 6.0.0.beta2, < 6.1)
activesupport (4.2.0.beta4)
i18n (>= 0.7.0.beta1, < 0.8)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.1)
tzinfo (~> 1.1)
GEM
remote: https://rubygems.org/
specs:
builder (3.2.2)
i18n (0.7.0.beta1)
json (1.8.1)
minitest (5.4.3)
pg (0.17.1)
thread_safe (0.3.4)
tzinfo (1.2.2)
thread_safe (~> 0.1)
PLATFORMS
ruby
DEPENDENCIES
activerecord!
arel!
pg
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.establish_connection(
{
adapter: "postgresql",
database: "onruby_development",
username: "postgres",
host: "localhost",
encoding: "utf8",
}
)
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts do |t|
t.string :name
end
create_table :comments do |t|
t.integer :post_id
t.string :name
end
end rescue nil
class Post < ActiveRecord::Base
has_many :comments
default_scope -> { where(name: 'test') }
end
class Comment < ActiveRecord::Base
belongs_to :post
default_scope -> { joins(:post).where("posts.name" => 'test').readonly(false) }
end
class BugTest < Minitest::Test
def test_association_stuff
post = Post.create!(name: 'test')
post.comments << Comment.create!(name: 'test')
assert_equal post.comments.map(&:id), Post.find(post.id).comments.map(&:id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment