Ruby on rails Rails导入JSON未在postgresql中创建对象

Ruby on rails Rails导入JSON未在postgresql中创建对象,ruby-on-rails,json,postgresql,activerecord,Ruby On Rails,Json,Postgresql,Activerecord,所以我要导入一个JSON文件,但是当我去创建对象时,并没有生成id 以下是我的导入方法: namespace :fieldfacts do desc "Import Topics" task :import_topics => :environment do records = JSON.parse(File.read('public/topics.json')) records.each do |record| a = Topic.create

所以我要导入一个JSON文件,但是当我去创建对象时,并没有生成id

以下是我的导入方法:

namespace :fieldfacts do
  desc "Import Topics"
  task :import_topics => :environment do
    records = JSON.parse(File.read('public/topics.json'))
    records.each do |record|
      a = Topic.create
      a.name = record['name']
      a.save
    end
  end
end
以下是错误:

我不认为没有身份证就可以保存它。那么我怎么才能在里面找到身份证呢?谢谢

编辑:根据主题模型,我需要通过主题所有者设置一个用户id,但是我该怎么做呢

主题模型

  has_many :topics_owners
  has_many :users, -> { uniq }, through: :topics_owners
  validates :name, presence: true
  validates :user_ids, presence: {:message => "There must be an assigned Topic Owner to continue."}
  validates :name, uniqueness: true
控制器

def topic_params
      params.require(:topic).permit(:name, :description, :active, [keywords: [:id, :keyword]], [keywords_deleted: [:keyword]], :keywords_list, :keywords_list_deleted,
        :organizations_list, :organizations_list_deleted, [organizations: [:id, :organization]], [organizations_deleted: [:organization]],
        :social_groups_list, :social_groups_list_deleted, [social_groups: [:id, :social_group]], [social_groups_deleted: [:social_group]],
        :feeds_list, :feeds_list_deleted, [feeds: [:id, :feed, :source_url]], [feeds_deleted: [:feed]],
        {:articles_list => []}, :articles_list_deleted, :influencers, {:feeds_list => []},
        :articles_object, {:feeds_object => []}, :topic_api_id, :read_only, {user_ids: []}, {:influencers_ids => []},
        [:people_relevants => [:name, :id, :influencer_id, :topic_id]],
        [influencers_adds: [:id, :weight, :influencers_add]], [:influencers_deleted => [:id]], [:influencers_list_deleted => [:id => []]],
        [people: [:id, :person, :weight]], [people_deleted: [:person]],
        [influencers_topics_attributes: [:influencer_id, :id, :_destroy]], :updater_id, :creator_id,
        [influencer_attributes: [:name, :active]])
    end
Schema.rb

  create_table "topics", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "influencers_topics_count", default: 0,     null: false
    t.text     "description"
    t.integer  "creator_id"
    t.integer  "updater_id"
    t.boolean  "read_only"
    t.string   "topic_api_id"
    t.integer  "people_relevants_count"
    t.boolean  "active",                   default: false
    t.datetime "activation_date"
  end

  add_index "topics", ["name"], name: "index_topics_on_name", using: :btree

  create_table "topics_owners", force: true do |t|
    t.integer  "topic_id"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "topics_owners", ["topic_id"], name: "index_topics_owners_on_topic_id", using: :btree
  add_index "topics_owners", ["user_id"], name: "index_topics_owners_on_user_id", using: :btree

您可能应该使用
a.save
而不是
a.save
(与
create
相同)。非bang返回成功/失败的
true
false
(您将忽略)。bang版本在失败时引发异常。这会让你马上知道出了什么问题


另外,如果您想找出问题所在,可以检查
a.errors
,尤其是
a.errors.full_消息
(使用非bang
保存
创建
,这样您仍然可以引用
a
)。在这种情况下,问题是您的某些验证失败(
需要用户ID
,也可能是非唯一的
名称
)。

您对
主题
模型有任何验证吗?这在主题模型中。validates:name,presence:true validates:user_id,presence:{:message=>“必须有一个指定的主题所有者才能继续。”}validates:name,university:true验证是您的问题。启动
rails控制台
,然后尝试这些命令。你会明白到底发生了什么。尝试使用
a=Topic.new
而不是
a=Topic.create
并在保存前构建
a
。酷,我已经尝试过了。谢谢或者,如果您可以在创建过程中传递属性,这可能会为您节省几行代码,例如
Topic.create(名称:“…”,用户ID:“…”)
谢谢您的提示。错误告诉我需要--validates:user_id,presence:{:message=>“必须有一个指定的主题所有者才能继续。”}。你能告诉我如何在我的任务中做到这一点吗?