Created
June 27, 2015 18:24
-
-
Save tovodeverett/51c20888616bc0702ffa to your computer and use it in GitHub Desktop.
Demonstrates change in chained order behavior with chained associations in Rails 4.2.3
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
begin | |
require 'bundler/inline' | |
rescue LoadError => e | |
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler' | |
raise e | |
end | |
gemfile(true) do | |
source 'https://rubygems.org' | |
# Activate the gem you are reporting the issue against. | |
gem 'activerecord', '4.2.3' | |
gem 'sqlite3' | |
end | |
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.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :rolls, force: true do |t| | |
end | |
create_table :photos, force: true do |t| | |
t.integer :roll_id | |
t.integer :idx | |
end | |
create_table :psets, force: true do |t| | |
end | |
create_table :pset_photos, force: true do |t| | |
t.integer :pset_id | |
t.integer :idx | |
t.integer :photo_id | |
end | |
end | |
class Roll < ActiveRecord::Base | |
has_many :photos, -> { order('photos.idx') } | |
end | |
class Photo < ActiveRecord::Base | |
belongs_to :roll | |
end | |
class Pset < ActiveRecord::Base | |
has_many :pset_photos, -> { order('idx') } | |
has_many :photos, -> { order('pset_photos.idx') }, through: :pset_photos | |
end | |
class PsetPhoto < ActiveRecord::Base | |
belongs_to :pset | |
belongs_to :photo | |
end | |
class BugTest < Minitest::Test | |
def test_association_stuff | |
roll = Roll.create! | |
roll.photos << Photo.create!(idx: 1) | |
roll.photos << Photo.create!(idx: 2) | |
(photo1, photo2) = roll.photos | |
pset = Pset.create! | |
pset.pset_photos << PsetPhoto.create!(idx: 1, photo_id: photo2.id) | |
pset.pset_photos << PsetPhoto.create!(idx: 2, photo_id: photo1.id) | |
assert_equal [photo2, photo1], pset.photos | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment