Plural Name in Association in Rails
Asked Answered
P

1

5

I have user model and a car model

I want to have a model which will hold the settings for each car and each user

so I do

class CarSettings < ActiveRecord::Base

  belongs_to :user
  belongs_to :car
end

for user:

  has_many :car_settings

and for cars:

  has_many :car_settings
  has_many :users, :through => :car_settings

note the name CarSettings, this isn't a mistake, I want it to be settings and not setting

When I do

c=Car.first
c.users

I get

NameError: uninitialized constant Car::CarSetting
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.12/lib/active_record/inheritance.rb:111:in `compute_type'

it is looking for a singular car_setting and not car_settings.

How can I fix this?

Perjured answered 6/3, 2013 at 18:2 Comment(4)
Could this help? #8525009Architectonics
I just saw it, added an inflection still causing it.. I added inflect.uncountable %w(car_settings)Perjured
Sorry if this sounds dumb, did you restart Rails?Architectonics
yup, I see the inflection works since it doesn't give me car_setting and car_settings, but I still get that errorPerjured
S
7

You can force the class name on the association using this option:

has_many :car_settings, :class_name => "CarSettings"
Scraper answered 6/3, 2013 at 18:18 Comment(1)
Rails will assume that all the model names are singular. So you need to force it to be plural.Scraper

© 2022 - 2024 — McMap. All rights reserved.