I need to create a join model called CarStoreTracker for Car and Store with both having many of each other.
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
The CarStoreTracker has
car_id and store_id on its table.
When I run:
> CarStoreTracker.first.car
> CarStoreTracker.first.store
They both work.
But
Store.first.cars
Car.first.stores
Store.carstoretrackers
Car.carstoretrackers
Non of them work.
NameError: uninitialized constant “CURRENTMODEL”::Carproducttracker
So, I scrapped the CarProductTracker and I just used the name Tracker for model and everything works.
What’s happening? What the name convention is for Rails in this case?
You need to put underscores after each word when defining has_many and other relationships.
So it would be :car_store_trackers