Skip to content

Instantly share code, notes, and snippets.

@mariozig
Last active December 23, 2015 06:29
Show Gist options
  • Save mariozig/6594685 to your computer and use it in GitHub Desktop.
Save mariozig/6594685 to your computer and use it in GitHub Desktop.
Add additional assert that more clearly exposes the fact that ArtistsTrack's artistic_role_id is being lost
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.0.0'
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.establish_connection(adapter: 'sqlite3', database: 'shiz.db')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :artists do |t|
end
create_table :tracks do |t|
end
create_table :artistic_roles do |t|
end
create_table :artists_tracks do |t|
t.integer :artist_id
t.integer :track_id
t.integer :artistic_role_id
end
end
class Track < ActiveRecord::Base
has_many :artists_tracks
has_many :owning_artists,
-> { where(:artists_tracks => { :artistic_role_id => 1 }) },
:through => :artists_tracks,
:source => :artist
end
class ArtistsTrack < ActiveRecord::Base
belongs_to :artist
belongs_to :track
belongs_to :artistic_role
end
class Artist < ActiveRecord::Base
has_many :artists_tracks
has_many :tracks, :through => :artists_tracks
end
class ArtisticRole < ActiveRecord::Base
has_many :artist_tracks
has_many :tracks, through: :artists_tracks
has_many :artists, through: :artists_tracks
end
track = Track.create!
artist = Artist.create!
role = ArtisticRole.create!
ArtistsTrack.create(track: track, artist: artist, artistic_role: role)
class ScopeTest < MiniTest::Unit::TestCase
def test_scoped_finding
assert_equal 1, Track.first.owning_artists.count
end
def test_scoped_creation
Track.create!(owning_artist_ids: [1])
assert_equal 1, ArtistsTrack.last.artistic_role_id
assert_equal 2, Track.count
assert_equal 1, Track.last.owning_artist_ids.length
assert_equal 1, Track.last.owning_artist_ids.first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment