Home > Enterprise >  Rails db:seed not seeding data
Rails db:seed not seeding data

Time:06-24

In my seeds.rb, User and Order are being seeded but CartItem, Item, and Cart are not. I am doing rails db:migrate db:seed and I don't see any errors in the terminal. Is it something wrong with my model relationships not matching up with what's in the schema or seeds file?

I've attached my model relationships, seeds file, and schema below.

Thanks for your help!

class CartItem < ApplicationRecord
    belongs_to :cart
    belongs_to :item
end

class Item < ApplicationRecord
    belongs_to :order
end

class Order < ApplicationRecord
    has_one :cart 
    belongs_to :user
    has_many :items, through: :cart
end
class User < ApplicationRecord
    has_many :orders
class Cart < ApplicationRecord
    has_many :cart_items, dependent: :destroy
    has_many :items, through: :cart_items
    has_one :user, through: :order
    belongs_to :order
end

User.destroy_all
Item.destroy_all
Cart.destroy_all
CartItem.destroy_all
Order.destroy_all

puts "Seeding Users...."

    u1 = User.create(username: 'joe' , email: "[email protected]", password: '123' , dob: "20-05-1990", name: "Joe")
    u2 = User.create(username: 'sam' , email: "[email protected]", password: '123' , dob: "10-01-1980", name: "Sam")
    u3 = User.create(username: 'david' , email: "[email protected]", password: '123' , dob: "30-03-1987", name: "David")

puts "Done Seeding Users!"

puts "Seeding Items...."


    i1 = Item.create(location: "Address1", image_url: "https://m.media-amazon.com/images/I/71xNX HQrAL._AC_UX679_.jpg", title: "Funcle Shirt", category: "Uncle", description: "Faker::Lorem.paragraph", user_id: u1.id)

    i2 = Item.create(location: "Address2", image_url: "https://m.media-amazon.com/images/I/A1ntnF3PJOL._CLa|2140,2000|71mT4TQux8L.png|0,0,2140,2000+0.0,0.0,2140.0,2000.0_AC_SX679._SX._UX._SY._UY_.png", title: "Cheesy Shirt", category: "Cheese", description: "Faker::Lorem.paragraph", user_id: u1.id)

    i3 = Item.create(location: "Address3", image_url: "https://ctl.s6img.com/society6/img/C931lpQ2OzPDyEVSZ1v4facuX2g/w_700/tshirts/men/greybg/black/~artwork,bg_FFFFFFFF,fw_3302,fh_5096,fx_-407,fy_-46,iw_3900,ih_3409/s6-original-art-uploads/society6/uploads/misc/325c18d3eebc4376b9cb3a5d23e33965/~~/punny-pineapples-tshirts.jpg", title: "Pineapple Shirt", category: "Fruit", description: "Faker::Lorem.paragraph", user_id: u2.id)

    i4 = Item.create(location: "Address4", image_url: "https://m.media-amazon.com/images/I/A13usaonutL._CLa|2140,2000|61zt9K4Cw4L.png|0,0,2140,2000+0.0,0.0,2140.0,2000.0_AC_UX679_.png", title: "Tangerine", category: "Fruit", description: "Faker::Lorem.paragraph", user_id: u2.id)

    i5 = Item.create(location: "Address5", image_url: "https://i0.wp.com/shirts-n-giggles.com/wp-content/uploads/2017/01/mockup-f7821e65.jpg?fit=600,600&ssl=1", title: "Chemistry Shirt", category: "Science", description: "Faker::Lorem.paragraph" , user_id: u3.id)

    puts "Done Items...."
    

    puts "Seeding Carts ...."

    Cart.create(order_id: 1)
    Cart.create(order_id: 2)

    puts "Done Seeding Carts! "
    

    puts "Seeding Orders"

    Order.create(user_id: 1, checked_out: false)

    puts "Done Seeding Orders!"


    puts "Seeding Cart Items ...."

    CartItem.create(cart_id: 1, item_id: 1, quantity: 1)
    CartItem.create(cart_id: 1, item_id: 4, quantity: 1)

    puts "Done Seeding Cart Items!"
   
ActiveRecord::Schema[7.0].define(version: 2022_06_23_215526) do
  create_table "cart_items", force: :cascade do |t|
    t.integer "cart_id"
    t.integer "item_id"
    t.integer "quantity"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "carts", force: :cascade do |t|
    t.integer "order_id", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["order_id"], name: "index_carts_on_order_id"
  end

  create_table "items", force: :cascade do |t|
    t.string "location"
    t.string "image_url"
    t.string "description"
    t.string "category"
    t.integer "user_id"
    t.string "title"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "orders", force: :cascade do |t|
    t.integer "user_id", null: false
    t.boolean "checked_out"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["user_id"], name: "index_orders_on_user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "name"
    t.string "username"
    t.string "dob"
    t.string "email"
    t.string "password_digest"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_foreign_key "carts", "orders"
  add_foreign_key "orders", "users"
end

CodePudding user response:

While creating Order, Cart, and CartItem, you passed hardcoded ids. Use the created object's reference instead.

Every time you run the seed, it will destroy the old records and create new ones with new ids.

User.destroy_all
Item.destroy_all
Cart.destroy_all
CartItem.destroy_all
Order.destroy_all

puts "Seeding Users...."

u1 = User.create(username: 'joe' , email: "[email protected]", password: '123' , dob: "20-05-1990", name: "Joe")
u2 = User.create(username: 'sam' , email: "[email protected]", password: '123' , dob: "10-01-1980", name: "Sam")
u3 = User.create(username: 'david' , email: "[email protected]", password: '123' , dob: "30-03-1987", name: "David")

puts "Done Seeding Users!"

puts "Seeding Items...."

i1 = Item.create(location: "Address1", image_url: "https://m.media-amazon.com/images/I/71xNX HQrAL._AC_UX679_.jpg", title: "Funcle Shirt", category: "Uncle", description: "Faker::Lorem.paragraph", user_id: u1.id)

i2 = Item.create(location: "Address2", image_url: "https://m.media-amazon.com/images/I/A1ntnF3PJOL._CLa|2140,2000|71mT4TQux8L.png|0,0,2140,2000+0.0,0.0,2140.0,2000.0_AC_SX679._SX._UX._SY._UY_.png", title: "Cheesy Shirt", category: "Cheese", description: "Faker::Lorem.paragraph", user_id: u1.id)

i3 = Item.create(location: "Address3", image_url: "https://ctl.s6img.com/society6/img/C931lpQ2OzPDyEVSZ1v4facuX2g/w_700/tshirts/men/greybg/black/~artwork,bg_FFFFFFFF,fw_3302,fh_5096,fx_-407,fy_-46,iw_3900,ih_3409/s6-original-art-uploads/society6/uploads/misc/325c18d3eebc4376b9cb3a5d23e33965/~~/punny-pineapples-tshirts.jpg", title: "Pineapple Shirt", category: "Fruit", description: "Faker::Lorem.paragraph", user_id: u2.id)

i4 = Item.create(location: "Address4", image_url: "https://m.media-amazon.com/images/I/A13usaonutL._CLa|2140,2000|61zt9K4Cw4L.png|0,0,2140,2000+0.0,0.0,2140.0,2000.0_AC_UX679_.png", title: "Tangerine", category: "Fruit", description: "Faker::Lorem.paragraph", user_id: u2.id)

i5 = Item.create(location: "Address5", image_url: "https://i0.wp.com/shirts-n-giggles.com/wp-content/uploads/2017/01/mockup-f7821e65.jpg?fit=600,600&ssl=1", title: "Chemistry Shirt", category: "Science", description: "Faker::Lorem.paragraph" , user_id: u3.id)

puts "Done Items...."

puts "Seeding Orders"

order1 = Order.create(user_id: u1.id, checked_out: false)
order2 = Order.create(user_id: u2.id, checked_out: false)

puts "Done Seeding Orders!"

puts "Seeding Carts ...."

cart1 = Cart.create(order_id: order1.id)
cart2 = Cart.create(order_id: order1.id)

puts "Done Seeding Carts! "

puts "Seeding Cart Items ...."

CartItem.create(cart_id: cart1.id, item_id: i1.id, quantity: 1)
CartItem.create(cart_id: cart1.id, item_id: i4.id, quantity: 1)

puts "Done Seeding Cart Items!"

CodePudding user response:

Your .create may be failing because of validation issues. Validation issues will not show up as errors with the .create function. Change your .create to .create! and it will raise an exception for validation errors and may show you your issue.

Alternatively, you can check #{instance_object}.errors and see if it is true or false. If it is false, it is failing to be saved.

  • Related