ruby-on-rails-3 - Ruby on Rails 通过集合对has_many进行分组的问题

  显示原文与译文双语对照的内容
52 5

i: 组和:uniq标记作为一个选项,通过集合定义来进行选择。我在系统中定义了许多三重模型关联,并且希望列出分组的元素以避免( 实际的) 重复。我的主要模型如下所示:

class Trip <ActiveRecord::Base
 belongs_to :agent
 has_many :trips_destinations, :class_name =>"TripsDestination"
 has_many :destinations, :through => :trips_destinations
 has_and_belongs_to_many :vibes, join_table: :trips_vibes
 has_and_belongs_to_many :verbs, join_table: :trips_verbs
 has_many :trips_destinations_activities, :class_name =>"TripsDestinationsActivity"
 has_many :activities, :through => :trips_destinations_activities, :uniq => true
 has_many :trips_destinations_hotels, :class_name =>"TripsDestinationsHotel"
 has_many :hotels, :through => :trips_destinations_hotels
 has_many :trips_destinations_recommended_places, :class_name =>"TripsDestinationsRecommendedPlace"
 has_many :recommended_places, :through => :trips_destinations_recommended_places
 has_many :trips_destinations_transportations, :class_name =>"TripsDestinationsTransportation"
 has_many :transportations, :through => :trips_destinations_transportations
...
...
end

任何人知道如何列出他们为什么要避免重复给定的[trip_id, destination_id]?为什么要重复?

我的Rails 版本为 4,下面的图像显示了将分组选项传递给has_many关系时的错误消息。

enter image description here

请帮助!

时间:原作者:0个回答

144 2

你的uniq语法对于 Rails 4是不正确的。

has_many :activities, -> { uniq }, :through => :trips_destinations_activities

Unique现在有了作用域语法。

原作者:
...