I am having a problem with relationships in Rails 4.
There are 4 models
- User
- Request
- Make
- Models
A User has_many Requests, a Request has_one Make and a Make has_many Models.
The User->Requests and Make->Models has_many relationships are fine but the Request->Make has_one relationship is failing.
class Request < ActiveRecord::Base
belongs_to :user
has_one :make
end
class Make < ActiveRecord::Base
has_many :models
belongs_to :request
end
The Schemas for each model are...
create_table "requests", force: true do |t|
t.integer "user_id"
t.integer "make_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "makes", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
When I try to assign a Make to a Request I get the following error
Mysql2::Error: Unknown column 'makes.request_id' in 'where clause': SELECT `makes`.* FROM `makes` WHERE `makes`.`request_id` = 7 LIMIT 1
ActiveRecord::StatementInvalid Exception: Mysql2::Error: Unknown column 'makes.request_id' in 'where clause': SELECT `makes`.* FROM `makes` WHERE `makes`.`request_id` = 7 LIMIT 1
nil
Why is ActiveRecord requiring a request_id in Make? Wouldn't that only be applicable for a has_many relationship as I have in the User->Requests and Makes->Models relationships?