Rails 6.0(初级(运行rails db:seed --trace
后,我的种子文件没有保存到我的数据库中
我得到以下输出:
** Invoke db:seed (first_time)
** Invoke db:load_config (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:load_config
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke db:load_config
** Execute db:abort_if_pending_migrations
因为它声明如果挂起迁移则中止。为了安全起见,我运行了rails-db:migrate,但它们都是最新的,所以什么都没做。
load_config
中有什么我需要做的吗?
注意:在这个过程中,我在这个表中创建了两个条目,同时试图让它发挥作用。表中先前的条目会阻止种子工作吗?
下方的种子文件
def time_rand from = 0.0, to = Time.now
Time.at(from + rand * (to.to_f - from.to_f))
end
event_list = [
["My headline",
"this is a very long description of text",
"Physical Location",
"Locations url",
time_rand,
"this is a shorter description of words",
"https://www.ticketmaster.com/",
"google maps url",
"123 Fake Street",
"Brooklyn",
"11123",
"NY",
"USA" ]
]
event_list.each do |headline, d_long, location_name, location_url, image_url, time, d_short, tickets_url, map_url, location_street, location_city, location_zip, location_state, location_country|
temp = Event.create(
headline: headline,
description_long: d_long,
location_name: location_name,
location_url: location_url,
time: time,
description_short: d_short,
tickets_url: tickets_url,
map_url: map_url,
location_street: location_street,
location_city: location_city,
location_zip: location_zip,
location_state: location_state,
location_country: location_country
)
temp.save
end
编辑:感谢Sebastian调用模型验证。尽管我认为我仍在完成验证,但这里的模型文件是:
class Event < ApplicationRecord
validates :headline, presence: true, length: { minimum: 3 }
validates :description_short, presence: true, length: { minimum: 3 }
validates :location_name, presence: true
validates :time, presence: true
end
多亏了Sebastian Palma建议使用byebug,我明白了:-我错过了params的计数,所以时间被赋予了一个字符串。-我在测试时间前也得到了零分,因此被验证拒绝了。我最终只是在某个时间值中进行了硬编码,以确保它最终通过。
经验教训:计算您的参数&使用byebug