Codejoy
Codejoy

Reputation: 3816

Rails association, I want something but the other model shouldn't care

So I think I need help with what I am doing with my asscoiations. I have two models (well many but for the purposes here). One is a Provider. Each provider can have a designation. Though the Designations is just a list it can appear in many different providers so it is not one to one. My models are as follows:

Provider.rb

class Provider < ActiveRecord::Base
 has_and_belongs_to_many :groups
 has_one :designation
 has_one :specialty
end

Designation.rb

class Designation < ActiveRecord::Base

end

The error I get is this: SQLite3::SQLException: no such column: designations.provider_id: SELECT "designations".* FROM "designations" WHERE "designations"."provider_id" = ? LIMIT 1

Which tells me my associations are off cause the designations should not have the provider_id, if anything the provider should have the designation_id.

Do I ditch the has_one and just put has_many in the Designation?

If I do that, I am new to rails, do I have to create a migration so the database is correctly updated?

Upvotes: 1

Views: 30

Answers (1)

Carpela
Carpela

Reputation: 2195

Try using

models/provider.rb

belongs_to :designation

Then in

models/designation.rb

has_many :providers

It may feel a little strange but the belongs_to just lets you know which model the id column needs to go in. In this case the provider model, so you'll need:

rails g migration add_designation_id_to_provider designation_id:integer

Upvotes: 1

Related Questions