Skip to content

Instantly share code, notes, and snippets.

@axsuul
Forked from phoet/gist:6916641
Last active December 25, 2015 05:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save axsuul/6924664 to your computer and use it in GitHub Desktop.
Save axsuul/6924664 to your computer and use it in GitHub Desktop.
unless File.exists?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', '4.0.0'
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 :name
end
create_table :comments do |t|
t.integer :post_id
t.integer :ownable_id
t.string :ownable_type
end
create_table :users do |t|
t.string :email
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :ownable, polymorphic: true
end
class User < ActiveRecord::Base
has_many :comments, as: :ownable
end
class EagerLoadTest < MiniTest::Unit::TestCase
def test_association_stuff
user = User.create!
post = Post.create! name: "Hello World"
Comment.create!(post: post, ownable: user)
assert_equal [1], Post.includes(comments: :ownable).map(&:id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment