Rails 3.1带有has_many的模型通过命名问题 - ThisIsModelName.rb

Chi*_*Kan 0 ruby model ruby-on-rails naming-conventions has-many-through

我需要为Car和Store创建一个名为CarStoreTracker的连接模型,它们彼此之间有很多.

class Car < ActiveRecord::Base
  has_many :carstoretrackers        # It seems to work
  has_many :stores, :through => :carstoretrackers  # I bet the naming is not being recognized by Rails convention
end

class Store < ActiveRecord::Base
  has_many :carstoretrackers        # It seems to work
  has_many :cars, :through => :carstoretrackers  # Same issue
end

class CarStoreTracker < ActiveRecord::Base
  belongs_to :store
  belongs_to :car
end
Run Code Online (Sandbox Code Playgroud)

CarStoreTracker有

car_id and store_id on its table.
Run Code Online (Sandbox Code Playgroud)

当我跑:

 > CarStoreTracker.first.car
 > CarStoreTracker.first.store
Run Code Online (Sandbox Code Playgroud)

他们都工作.

Store.first.cars Car.first.stores Store.carstoretrackers Car.carstoretrackers

他们没有工作.NameError:未初始化的常量"CURRENTMODEL":: Carproducttracker

所以,我取消了CarProductTracker,我只使用了Tracker这个名称来模型,一切正常.

发生了什么?在这种情况下,Rails的名称约定是什么?

Wah*_*Ali 5

在定义has_many和其他关系时,您需要在每个单词后面加下划线.

所以它将是:car_store_trackers