ActiveRecord找不到现有记录

时间:2017-02-27 23:27:01

标签: ruby-on-rails activerecord

如果我尝试创建新的“重新填充”记录,但我收到此错误消息:

Couldn't find Vehicle with ID=1 for Refill with ID=

但如果我在SQLite3数据库中进行直接选择,车辆记录就在那里。

以下是相关代码:

车辆类:

class Vehicle < ApplicationRecord
    has_many :refills, :inverse_of=> :vehicle
    validates_presence_of :prefix, :plate, :diesel_type
end

补充课程:

class Refill < ApplicationRecord
    belongs_to :vehicle
    validates_presence_of :date, :mileage, :liters, :vehicle
    accepts_nested_attributes_for :vehicle
end

refill_params方法:

def refill_params
  params.require(:refill).permit(:date, :mileage, :liters, vehicle_attributes: [:id])
end

json我正试图发帖:

{
    "refill": {
        "date": "2017-01-01",
        "mileage": 125000,
        "liters": 115,
        "vehicle_attributes": {
            "id": 1
        }
    }
}

0 个答案:

没有答案