我有两个模型-CallDetail和Agent。代理表使用call_details表中的外键。

该模式粘贴在下面。

ActiveRecord::Schema.define(version: 20170824171227) do

  create_table "agents", force: :cascade do |t|
    t.integer "agent_id"
    t.string "name"
    t.integer "CallDetail_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["CallDetail_id"], name: "index_agents_on_CallDetail_id"
  end

  create_table "call_details", force: :cascade do |t|
    t.integer "call_id"
    t.string "word"
    t.float "start_time"
    t.float "end_time"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end


在上述数据库模式中,除了默认显示的属性外,rails默认还会创建CallDetail ID和Agent ID属性。

我的Agents控制器的create函数如下所示

def create
    @agent = Agent.new(agent_params)

    respond_to do |format|
      if @agent.save
        format.html { redirect_to @agent, notice: 'Agent was successfully created.' }
        format.json { render :show, status: :created, location: @agent }
      else
        format.html { render :new }
        format.json { render json: @agent.errors, status: :unprocessable_entity }
      end
    end
  end


我的模型如下

class CallDetail < ApplicationRecord
end
class Agent < ApplicationRecord
  belongs_to :CallDetail
end


当我打开localhost:3000 / agents并尝试在数据库中创建新条目时,显示以下错误。

SQLite3 :: SQLException:没有此类表:main.CallDetails:插入“代理”(“ agent_id”,“ name”,“ CallDetail_id”,“ created_at”,“ updated_at”)VALUES(?,?,?,?,? )

enter image description here

我是新手。我在这里做错了什么?请帮忙。

最佳答案

Rails使用变形来更改模型名称(除其他外)。您错误地引用了CallDetail。引用CallDetail时,应使用snake_case而不是CamelCase。您在应调用call_detail的位置调用CallDetail。

注意,在架构文件中,CallDetail已转换为call_details。这是Rails使用变形的一种方式。您可以阅读有关变形的信息here

关于mysql - 无法使用 Controller 的创建功能在数据库中创建条目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45873383/

10-11 08:14