0

我需要为Car和Store创建一个名为CarStoreTracker的连接模型,它们之间有许多关系。通过命名问题使用has_many的Rails 3.1模型 - ThisIsModelName.rb

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 

的CarStoreTracker有

car_id and store_id on its table. 

当我运行:

> CarStoreTracker.first.car 
> CarStoreTracker.first.store 

他们都工作。

他们的

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

非工作。 NameError:未初始化的常量“CURRENTMODEL”:: Carproducttracker

因此,我放弃了CarProductTracker,并且只使用名称Tracker作为模型,一切正常。

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

回答

5

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

所以它会是:car_store_trackers

+0

感谢您的提示。很难找到它。 – 2012-01-10 03:49:22