Created
May 8, 2014 09:28
-
-
Save sudhirj/2118db36325ed7e50e68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unless File.exist?('Gemfile') | |
File.write('Gemfile', <<-GEMFILE) | |
source 'https://rubygems.org' | |
gem 'rails', '~> 4.1' | |
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: 'bugtest') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :movies do |t| | |
end | |
create_table :languages do |t| | |
end | |
create_table :cities do |t| | |
end | |
create_table :theatres do |t| | |
t.references :city | |
end | |
create_table :shows do |t| | |
t.references :language | |
t.references :movie | |
end | |
create_table :schedules do |t| | |
t.references :show | |
t.datetime :schedule_time | |
t.references :theatre | |
end | |
end | |
class Movie < ActiveRecord::Base | |
has_many :schedules, through: :shows | |
has_many :shows | |
end | |
class Schedule < ActiveRecord::Base | |
belongs_to :show | |
scope :latest, -> { where(schedule_time: 2.minutes.ago)} | |
belongs_to :theatre | |
end | |
class Show < ActiveRecord::Base | |
has_many :schedules | |
belongs_to :language | |
belongs_to :movie | |
end | |
class Theatre < ActiveRecord::Base | |
has_many :shows | |
belongs_to :city | |
has_many :schedules, through: :shows | |
end | |
class City < ActiveRecord::Base | |
has_many :theatres | |
end | |
class Language < ActiveRecord::Base | |
has_many :shows | |
end | |
class BugTest < Minitest::Test | |
def test_nested_includes | |
movie = Movie.create! | |
# Doesn't work - needs `references` | |
# schedules = Movie.first.schedules.latest | |
# .includes(:show).includes(theatre: [:city]) | |
# .where('cities.id = ?', 42) | |
# .to_a | |
# Works, But it makes absolutely no difference what you actually reference. | |
schedules = Movie.first.schedules.latest | |
.includes(:show).includes(theatre: [:city]) | |
.where('cities.id = ?', 42) | |
.references(:whatever_trolololollo).to_a | |
# Stops working, as soon as nested associations are eagerly loaded. | |
schedules = Movie.first.schedules.latest | |
.includes(show: [:language]).includes(theatre: [:city]) | |
.where('cities.id = ?', 42) | |
.references(:useless_symbol).to_a | |
# HINT: Perhaps you meant to reference the table alias "shows_schedules". | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment