0

我试图找出我认为是 Rails 中 has_and_belongs_to_many 方法中的错误。问题是我在课堂上使用了不规则的复数形式。我们将把这个类称为 StreetOfCars。我正确设置了不规则复数,如下所述。类名只是示例,重要的是 StreetOfCars 的不规则性并且它以“s”结尾。我有两个问题。

  1. 这到底是怎么回事?
  2. 超级终于降落在哪里?

如果您需要任何其他信息,请告诉我。

StreetOfCars.reflect_on_assocation(:cities).options[:join_table]
#=> 'cities_streets_of_cars'

City.reflect_on_assocation(:streets_of_cars).options[:join_table]
#=> 'cities_streets_of_car'

class StreetOfGold
  has_and_belongs_to_many :cities
end

class City
 has_and_belongs_to_many :streets_of_gold
end

#File config/initializers/inflections.rb
   ActiveSupport::Inflector.inflections do |inflect|
     inflect.irregular 'street_of_gold', 'streets_of_gold'
   end

# File activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb, line 7
  class HasAndBelongsToMany < CollectionAssociation
    def build
      reflection = super
      check_validity(reflection)
      define_destroy_hook
      reflection
    end
  end

# File activerecord/lib/active_record/associations/builder/collection_association.rb, line 21
  class CollectionAssociation < Association
    def build
      wrap_block_extension
      reflection = super
      CALLBACKS.each { |callback_name| define_callback(callback_name) }
      reflection
    end
  end
# File active_record/associations/association.rb
  class Association
    # build is not defined in this class.
  end
4

0 回答 0