ruby 写入种子文件并获得语法错误和Null:Violation错误

qyuhtwio  于 2023-01-25  发布在  Ruby
关注(0)|答案(2)|浏览(115)

我尝试写一个种子文件来生成随机的Kudo。Kudo有一个标题,一个内容(都是用Fakergem生成的),但这些都不是问题。Kudo还有一个giver_id和一个receiver_id外键。我想让我的Kudo在不同的用户之间随机生成。我一直在盲目地试验不同的语法(我对rails超级陌生;))。因此,我的种子文件如下所示

employees = Employee.create!([{email: Faker::Internet.email(domain: 'gmail.com'), password: 'password'},{email: Faker::Internet.email(domain: 'gmail.com'), password: 'password'},...])

kudos = Kudo.create!(Title: Faker::Adjective.positive, Content: Faker::Company.bs, Kudo.new(giver:Employee.create()), Kudo.new(receiver:Employee.create()))

这就产生了语法错误

SyntaxError: /home/valar/Dokumenty/ERP_v1/db/seeds.rb:9: syntax error, unexpected ',', expecting => ...o.new(giver:Employee.create()), Kudo.new(receiver:Employee.c... ...                  /home/valar/Dokumenty/ERP_v1/db/seeds.rb:9: syntax error, unexpected ')', expecting end-of-input ...ew(receiver:Employee.create()) ... ... ...                            ^

我也试着让我的种子文件看起来像这样

kudos = Kudo.create!(Title: Faker::Adjective.positive, Content: Faker::Company.bs, giver:Employee.create(), receiver:Employee.create())

但我一直收到null:violation错误

rake aborted!
ActiveRecord::NotNullViolation: PG::NotNullViolation: ERROR:  null value in column "giver_id" violates not-null constraint

这是模特儿。

class Employee < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  has_many :given_kudos, class_name: 'Kudo', foreign_key: 'giver_id'
  has_many :received_kudos, class_name: 'Kudo', foreign_key: 'receiver_id'
end

工藤:

class Kudo < ApplicationRecord
  validates :Title, presence: true
  validates :Content, presence: true
  belongs_to :giver, class_name: 'Employee'
  belongs_to :receiver, class_name: 'Employee'
end

和我的模式文件:

ActiveRecord::Schema.define(version: 2023_01_20_162230) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "employees", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.index ["email"], name: "index_employees_on_email", unique: true
    t.index ["reset_password_token"], name: "index_employees_on_reset_password_token", unique: true
  end

  create_table "kudos", force: :cascade do |t|
    t.string "Title", null: false
    t.text "Content", null: false
    t.integer "giver_id", null: false
    t.integer "receiver_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

end

'
我试着乱加括号,花括号和方括号。有人能修复我的种子文件吗,因为它快把我逼疯了。我知道这是个新手问题,但对我这个学习阶段来说是个严重的问题。

fdx2calv

fdx2calv1#

下面是一个例子,说明如何更新种子文件,以正确创建一个具有随机给予者和接收者的随机Kudo:

employees = Employee.create!([{email: Faker::Internet.email(domain: 'gmail.com'), password: 'password'},{email: Faker::Internet.email(domain: 'gmail.com'), password: 'password'},...])

# randomly select a giver and receiver from the employees array
giver = employees.sample
receiver = employees.sample

# create the Kudo with the randomly selected giver and receiver
kudos = Kudo.create!(Title: Faker::Adjective.positive, Content: Faker::Company.bs, giver: giver, receiver: receiver)

这将从雇员数组中随机选择一个雇员作为新工藤的给予者和接受者。请确保在创建工藤之前已经创建了雇员。

gzszwxb4

gzszwxb42#

第一个语法错误是因为create需要哈希或哈希数组。
当你说:

kudos = Kudo.create!(Title: Faker::Adjective.positive, Content: Faker::Company.bs, Kudo.new(giver:Employee.create()), Kudo.new(receiver:Employee.create()))

您为“Title”和“Content”提供了键,但没有为最后两个参数提供键,因此它不是有效的哈希(或哈希数组)
如果您添加了“giver”和“receiver”键,那么您将传递语法错误(就像您得到第二个错误时所做的那样)。
第二个错误是,虽然您通过提供有效的哈希值粘贴了语法错误,但Employee.create中的一个或两个的ID()方法未能被保存到数据库,并且由create返回的结果对象对于thereid具有nil,这会导致模式中Kudogiver_d和reciever_id列的验证失败。我从来没有使用过Devise,但我假设它需要传递到create的东西,以便创建一个可以保存到数据库的有效雇员。

kudos = Kudo.create!(Title: Faker::Adjective.positive, Content: Faker::Company.bs, giver:Employee.create(), receiver:Employee.create())

相关问题