问题分组has_many通过收集

时间:2014-12-16 17:10:34

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

我遇到了以下问题:group和:uniq标签作为has_many通过集合定义的选项。我在我的系统上定义了许多三重模型关联,我想列出分组的元素以避免(实际)重复。我的主要模型看起来像这样:

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

请帮忙!

1 个答案:

答案 0 :(得分:1)

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

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

独特现在有scope syntax