I'm trying to write a seed file that will generate a random Kudo. A Kudo has a Title, a Content (both generated with Faker gem), but these are not the issue. Kudo has also a giver_id, and a receiver_id foreign keys. And I want to make my Kudo randomly generated between various users. And I've been kind of blindly experimenting with different syntax (I'm super new to rails ;)). So my seed file looks like this
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()))
And that gives me the syntax error
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()) ... ... ... ^
And i also tried to make my seed file look like this
kudos = Kudo.create!(Title: Faker::Adjective.positive, Content: Faker::Company.bs, giver:Employee.create(), receiver:Employee.create())
But I keep geting the null:violation error
rake aborted!
ActiveRecord::NotNullViolation: PG::NotNullViolation: ERROR: null value in column "giver_id" violates not-null constraint
Here are models. Emplyee
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
And Kudo:
class Kudo < ApplicationRecord
validates :Title, presence: true
validates :Content, presence: true
belongs_to :giver, class_name: 'Employee'
belongs_to :receiver, class_name: 'Employee'
end
And my schema file:
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
`
I tried messing with paretheses, curly braces and square bracets. Can someone please fix my seed file because it's driving me crazy. I know it's a noob problem but nevertheless is a serious problem for me at my stage of learning.
CodePudding user response:
Here is an example of how you could update your seed file to correctly create a random Kudo with a random giver and receiver:
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)
This will randomly select an employee from the employees array to be the giver and receiver for the new Kudo. Make sure that you have already created the employees before creating the kudos.
CodePudding user response:
The first syntax error is because create expects either a Hash or an Array of Hashes.
When you called this:
kudos = Kudo.create!(Title: Faker::Adjective.positive, Content: Faker::Company.bs, Kudo.new(giver:Employee.create()), Kudo.new(receiver:Employee.create()))
You provided the keys for "Title" and "Content" but then you did not provide keys for the last two arguments, so it's not a valid Hash (or Array of Hashes)
If you had added the "giver" and "receiver" keys then you would have gotten passed the syntax error (like you did when you got your second error).
Your second error is because while you did get pasted the syntax error by providing a valid hash, the id of one or both of the Employee.create() methods failed be saved to the database and the resulting object returned by create
has nil for there id, which fails the validation on the Kudo giver_d and reciever_id columns in the schema. I have never used Devise but I am assuming it needs something passed in to create
in order to create a valid Employee that can be saved to the database.
kudos = Kudo.create!(Title: Faker::Adjective.positive, Content: Faker::Company.bs, giver:Employee.create(), receiver:Employee.create())