Rails 5 hasMany通过是不是过滤
Rails 5 hasMany through is not filtering
我正在从 Rails 4 迁移到 Rails 5 并遇到了这个问题。我有 hasMany
through
关系来连接 Track
和 RightHolder
类:
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
在 Rails 4 中,这是完美的,但在 Rails 5 中,当我尝试获取:
Track.first.right_holders
RightHolder.first.tracks
Rails 应用程序执行这些查询:
select * from right_holders
select * from tracks
我做错了什么?这是一个错误吗?
在 Rails 5 中,关系 uniq
被弃用,取而代之的是 distinct
:
https://edgeguides.rubyonrails.org/5_0_release_notes.html#active-record-deprecations
更多信息见
只需将 { uniq }
更改为 { distinct }
我正在从 Rails 4 迁移到 Rails 5 并遇到了这个问题。我有 hasMany
through
关系来连接 Track
和 RightHolder
类:
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
在 Rails 4 中,这是完美的,但在 Rails 5 中,当我尝试获取:
Track.first.right_holders
RightHolder.first.tracks
Rails 应用程序执行这些查询:
select * from right_holders
select * from tracks
我做错了什么?这是一个错误吗?
在 Rails 5 中,关系 uniq
被弃用,取而代之的是 distinct
:
https://edgeguides.rubyonrails.org/5_0_release_notes.html#active-record-deprecations
更多信息见
只需将 { uniq }
更改为 { distinct }