【发布时间】:2014-11-30 23:59:06
【问题描述】:
我在 Rails 4 中遇到了关系问题。
有 4 种型号
- 用户
- 请求
- 制作
- 型号
一个 User has_many Requests,一个 Request has_one Make 和一个 Make has_many Models。
User->Requests 和 Make->Models has_many 关系很好,但 Request->Make has_one 关系失败。
class Request < ActiveRecord::Base
belongs_to :user
has_one :make
end
class Make < ActiveRecord::Base
has_many :models
belongs_to :request
end
每个模型的架构是...
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
当我尝试将 Make 分配给请求时,我收到以下错误
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
为什么 ActiveRecord 在 Make 中需要 request_id?这不只适用于我在 User->Requests and Makes->Models 关系中的 has_many 关系吗?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 activerecord