What do you want to do
I want to input data from the seed file into the table
problems and error messages occurring
Rails db:seed does not allow data input
I ran create on railsc and checked the contents, but I couldn't put it in.
[2]pry(main)>Product.create(id:1, product_name: 'Seafood Nabe Set (4 servings), product_price: '12800', image:File.open('./app/assets/images/uni.jpg')))
=>#<Product:0x00007f87f1b26008 id:1, product_price:12800, product_name: "Seafood Nabe Set (4 servings), image:"#<File:0x00007f87f1b260a8>", created_at:nil, updated_at:nil>
[3] pry(main)>Product.all
Product Load (0.5ms) SELECT `products`.* FROM `products`
= > [ ]
Related Source Code
seeds.rb
Product.create(id:1, product_name: 'Seafood Nabe Set (4 servings), product_price: '12800', image: File.open('./app/assets/images/uni.jpg')))
Product.create(id:2, product_name: 'Motsunabe Set (4 servings), product_price: '10800', image: File.open('./app/assets/images/motu.jpg')))
20210828062853_create_products.rb
class CreateProducts<ActiveRecord::Migration [6.0]
def change
create_table:products do|t|
t.integer:product_price
t.string —product_name
t.text —image
t.timestamps
end
end
end
schema.rb
ActiveRecord::Schema.define(version:2021_08_28_062853)do
create_table "menu", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do | t |
t. integer "price"
t.string "name"
t.datetime "created_at", precision:6, null:false
t.datetime "updated_at", precision:6, null:false
end
create_table "orders", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do | t |
t.date "arrival", null:false
t. integer "number", null: false
t. integer "product_name", null: false
t. integer "product_price", null: false
t.bigint "user_id", null:false
t.bigint "product_id", null:false
t.index["product_id", name: "index_orders_on_product_id"
t.index["user_id", name: "index_orders_on_user_id"
end
create_table "products", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do | t |
t. integer "product_price"
t.string "product_name"
t.text "image"
t.datetime "created_at", precision:6, null:false
t.datetime "updated_at", precision:6, null:false
end
create_table "reservations", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do | t |
t.time "time", null: false
t.date "date", null:false
t. integer "people", null: false
t.text "remark"
t. integer "person_price", null: false
t. integer "total_price", null: false
t.bigint "user_id", null:false
t.bigint "menu_id", null:false
t.datetime "created_at", precision:6, null:false
t.datetime "updated_at", precision:6, null:false
t.index["menu_id", name: "index_reservations_on_menu_id"
t.index["user_id", name: "index_reservations_on_user_id"
end
create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do | t |
t.string "email", default:", null:false
t.string "encrypted_password", default:", null:false
t.string "nickname", null:false
t.string "phone", null: false
t.string "postal_code", null:false
t.string "prefect", null:false
t.string "community", null: false
t.string "address", null: false
t.string "building"
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", precision:6, null:false
t.datetime "updated_at", precision:6, null:false
t.index["email", name: "index_users_on_email", unique: true
t.index["reset_password_token", name: "index_users_on_reset_password_token", unique: true
end
add_foreign_key "orders", "users"
add_foreign_key "reservations", "users"
end
Tried
created_at:nil, updated_at:nil
As it said, I ran all the information in the terminal, but I couldn't put it in.
[4]pry(main)>Product.create(id:1, product_name: 'Seafood Nabe Set (4 servings), product_price: '12800', image: File.open('./app/assets/images/uni.jpg'), created_at: 'Sat, 28 Aug 2021 07:14:20:00:20:20:20:20:20:20:20:20:20:20:20:20:20:20:20:20:20:20:20:20:20:20:20:20:20:20:20:20:20:20:20
=>#<Product:0x00007f881231f5c8
id: 1,
product_price: 12800,
product_name: "Seafood Nabe Set (4 servings),
image: "#<File:0x00007f881231f6e0>",
created_at:Sat, 28 Aug 2021 07:14:56 UTC + 00:00,
updated_at:Sat, 28 Aug 2021 07:14:56 UTC + 00:00>
↓
[5]pry(main)>Product.all
Product Load (0.8ms) SELECT `products`.* FROM `products`
= > [ ]
Language library version
Rails 6.0.4.1
I would appreciate it if you could let me know the other reasons why you have not been able to input anything.
ruby-on-rails mysql
Hello
I'm a beginner at Rails, but I'd like to make a comment to help you!
First of all, I understand that you cannot input sample data when you run rails db:seed
, but are there any errors when you run rails db:seed
?
By the way, id and created_at·updated_at are shaken freely by the rails, so it seems that it is not a good decision.
If I had written ysk0507's seed.rb, I would do it like this.
Product.create!(product_name: 'Seafood Nabe Set (4 servings), product_price: '12800', image:open(Rails.root.join('/app/assets/images/uni.jpg')))
c If you add !
after create, an exception will be issued and a message will be sent when an error occurs and you cannot save it.
Also, I think it would be better to identify the cause by looking at the error statement. What do you think?
© 2024 OneMinuteCode. All rights reserved.