我有一个Rails应用程序,其中包含“Datapoint”和“Dataset”模型对象。数据集与Datapoints相似。
模型/ dataset.rb:
# Table name: datasets
#
# id :integer not null, primary key
# name :string(255)
# created_at :datetime
# updated_at :datetime
class Dataset < ActiveRecord::Base
belongs_to :user
validates :name, :presence => true
end
模型/ datapoint.rb:
# Table name: datapoints
#
# id :integer not null, primary key
# dataset :integer
# date :date
# value :float
# created_at :datetime
# updated_at :datetime
class Datapoint < ActiveRecord::Base
belongs_to :dataset
validates :date, :presence => true
validates :dataset, :presence => true
validates :value, :presence => true
end
我的controllers / datapoints_controller.rb成功保存了数据点记录:
class DatapointsController < ApplicationController
def create
@datapoint = Datapoint.new(params[:datapoint])
@datapoint.dataset = Dataset.find(current_user.dataset)
if @datapoint.save
redirect_to root_path
else
end
end
end
但是,当我查看我的数据库时,每个数据点条目(这是必需的外键)的数据集字段为空。这怎么可能?
答案 0 :(得分:1)
看起来是错误的迁移(外键的字段名称错误):
# Table name: datapoints
# dataset :integer <<<< This should be dataset_id
# date :date
...