Rails Noob
Rails Noob

Reputation: 41

Rails 3 Model Setup

I have a company

The company can have one status

I am getting myself confused in how to set-up my associations in the models

Basically in the views, i will have a drop down of the statuses and the user selects one for each company...

companies
id
name
status_id

company_statuses 
id
status (Unknown, Live, Dissolved, etc...)

My way of thinking for using a company_statuses table is that they could change overtime and simply changing the name of the status in the table updates all companies at once etc...

Is this the rails way ?!, or would i be better hardcoding the values in the code and updating the database manually if changes need to be made ?!

Upvotes: 1

Views: 133

Answers (2)

Rails Noob
Rails Noob

Reputation: 41

Managed to get it working by using the following code. It is a has_many relationship not has_one

class Company < ActiveRecord::Base
  belongs_to :status_code
end

class StatusCode < ActiveRecord::Base
  has_many :companies
end

Upvotes: 1

Gerard
Gerard

Reputation: 4848

A company has one status right? If this is the case, the relationship is many to one and does not require a company_status link table. Simply configure your company table to have a status column.

My Rails 3 is a little rusty but your company model will feature:

has_one :status

And your Status model will have:

belongs_to :company

Upvotes: 0

Related Questions