3

I am migrating from Rails 4 to Rails 5 and got into this problem. I have the hasMany through relationship to connect the Track and the RightHolder classes:

class RightHolder < ActiveRecord::Base
  has_many :right_holder_tracks, class_name: 'TrackRightHolder', dependent: :delete_all
  has_many :tracks, -> { uniq }, through: :right_holder_tracks
end

class TrackRightHolder < ActiveRecord::Base
  belongs_to :track
  belongs_to :right_holder
end

class Track < ActiveRecord::Base
  has_many :track_right_holders, dependent: :destroy
  has_many :right_holders, -> { uniq }, through: :track_right_holders
end

In Rails 4 this was working perfectly, but in Rails 5, when I try to get:

Track.first.right_holders
RightHolder.first.tracks

The Rails application does these queries:

select * from right_holders
select * from tracks

What am I doing wrong? Is this a bug?

Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
Daniel Cukier
  • 11,502
  • 15
  • 68
  • 123

2 Answers2

3

In Rails 5, relation uniq was deprecated in favor of distinct : https://edgeguides.rubyonrails.org/5_0_release_notes.html#active-record-deprecations

More information in this answer

colinux
  • 3,989
  • 2
  • 20
  • 19
1

just change { uniq } to { distinct }

Kuo Jimmy
  • 801
  • 5
  • 13