Skip to content

Instantly share code, notes, and snippets.

@dmarkow
Created August 20, 2014 19:17
Show Gist options
  • Save dmarkow/c27d00bc4fb030f2dce5 to your computer and use it in GitHub Desktop.
Save dmarkow/c27d00bc4fb030f2dce5 to your computer and use it in GitHub Desktop.
ActiveRecord 4.2.0.beta1 Polymorphic includes
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'rack', github: 'rack/rack'
gem 'i18n', github: 'svenfuchs/i18n'
gem 'pg'
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: 'postgresql', database: 'test_ar')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :notes do |t|
t.string :notable_type
t.integer :notable_id
t.string :category
end
create_table :comments do |t|
t.integer :post_id
t.string :category
end
create_table :posts do |t|
end
end
class Post < ActiveRecord::Base
has_many :comments
has_many :notes, as: :notable
end
class Note < ActiveRecord::Base
belongs_to :notable, polymorphic: true
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class BugTest < Minitest::Test
def test_association_stuff
post = Post.create!
post.comments << Comment.create!(category: "Foo")
post.notes << Note.create!(category: "Foo")
# Works fine
assert_equal post, Post.includes(:comments).order("comments.category").first
assert_equal post, Post.includes(:comments).references(:comments).where("comments.category = ?", "Foo").first
# Both of these fail
assert_equal post, Post.includes(:notes).order("notes.category").first
assert_equal post, Post.includes(:notes).references(:notes).where("notes.category = ?", "Foo").first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment