Rails db:seed 如何绕过验证
Rails db:seed how to bypass validations
错误信息:
rails aborted!
ActiveRecord::RecordInvalid: Validation failed: Status : Event is Locked
我在 seeds.rb 中存储了一堆测试数据,我在使用 db:seed 播种时遇到了问题。
预测结构
{user_id: 2, event_id: 3, fighter_guess: "Jessica Andrade", method_guess: "Decision"},
{user_id: 2, event_id: 3, fighter_guess: "Claudio Puelles", method_guess: "Submission"},
{user_id: 2, event_id: 2, fighter_guess: "Vicente Luque", method_guess: "Decision"},
{user_id: 2, event_id: 2, fighter_guess: "Caio Borralho", method_guess: "TKO/KO"},
{user_id: 2, event_id: 1, fighter_guess: "Korean Zombie", method_guess: "TKO/KO"},
{user_id: 2, event_id: 1, fighter_guess: "Petr Yan", method_guess: "Decision"},
{user_id: 2, event_id: 4, fighter_guess: "Rob Font", method_guess: "Submission"},
{user_id: 2, event_id: 5, fighter_guess: "Charles Oliveira", method_guess: "Decision"},
{user_id: 2, event_id: 5, fighter_guess: "Rose Namajunas", method_guess: "Submission"},
{user_id: 2, event_id: 6, fighter_guess: "Aleksandar Rakic", method_guess: "Decision"},
...
我对预测模型进行了验证,在允许进行预测之前检查相关 event_id 的状态是否为“即将到来”。因此,当我 运行 db:seed 时,它无法通过验证,因为某些事件不是“即将发生”。
事件结构
{event_name: "UFC FN: LEMOS x ANDRADE", f1: "Amanda Lemos", f2: "Jessica Andrade", f3: "Clay Guida", f4: "Claudio Puelles", status: "Concluded", date: "Sat, Apr 23"},
{event_name: "UFC FN: FONT x VERA", f1: "Rob Font", f2: "Marlon Vera", f3: "Andrei Arlovski", f4: "Jake Collier", status: "Concluded", date: "Sat, Apr 30"},
{event_name: "UFC 274", f1: "Charles Oliveira", f2: "Justin Gaethje", f3: "Rose Namajunas", f4: "Carla Esparza", status: "Upcoming", date: "Sat, May 7"},
Prediction.rb
class Prediction < ApplicationRecord
belongs_to :event
belongs_to :user
validate :prediction_no_longer_allowed
def prediction_no_longer_allowed
if self.event.concluded? || self.event.in_progress?
errors.add(:status, ": Event is Locked")
end
end
end
Event.rb
...
def concluded?
self.status == "Concluded"
end
def in_progress?
self.status == "In Progress"
end
对于我想要与其他人共享应用程序以测试功能的场景,如果我可以绕过验证以进行播种,将会节省大量时间。
我按以下顺序播种数据:
用户 --> 事件 --> 预测
你可以使用insert_all
方法
Inserts multiple records into the database in a single SQL INSERT statement. It does not instantiate any models nor does it trigger Active Record callbacks or validations. Though passed values go through Active Record's type casting and serialization.
Prediction.insert_all([
{user_id: 2, event_id: 3, fighter_guess: "Jessica Andrade", method_guess: "Decision"},
{user_id: 2, event_id: 3, fighter_guess: "Claudio Puelles", method_guess: "Submission"},
{user_id: 2, event_id: 2, fighter_guess: "Vicente Luque", method_guess: "Decision"},
{user_id: 2, event_id: 2, fighter_guess: "Caio Borralho", method_guess: "TKO/KO"},
{user_id: 2, event_id: 1, fighter_guess: "Korean Zombie", method_guess: "TKO/KO"},
{user_id: 2, event_id: 1, fighter_guess: "Petr Yan", method_guess: "Decision"},
{user_id: 2, event_id: 4, fighter_guess: "Rob Font", method_guess: "Submission"},
{user_id: 2, event_id: 5, fighter_guess: "Charles Oliveira", method_guess: "Decision"},
{user_id: 2, event_id: 5, fighter_guess: "Rose Namajunas", method_guess: "Submission"},
{user_id: 2, event_id: 6, fighter_guess: "Aleksandar Rakic", method_guess: "Decision"},
])
创建第一个特定事件和用户可能更好。然后调用user_id: my_super_user.id, event_id: my_super_event.id
等
错误信息:
rails aborted!
ActiveRecord::RecordInvalid: Validation failed: Status : Event is Locked
我在 seeds.rb 中存储了一堆测试数据,我在使用 db:seed 播种时遇到了问题。
预测结构
{user_id: 2, event_id: 3, fighter_guess: "Jessica Andrade", method_guess: "Decision"},
{user_id: 2, event_id: 3, fighter_guess: "Claudio Puelles", method_guess: "Submission"},
{user_id: 2, event_id: 2, fighter_guess: "Vicente Luque", method_guess: "Decision"},
{user_id: 2, event_id: 2, fighter_guess: "Caio Borralho", method_guess: "TKO/KO"},
{user_id: 2, event_id: 1, fighter_guess: "Korean Zombie", method_guess: "TKO/KO"},
{user_id: 2, event_id: 1, fighter_guess: "Petr Yan", method_guess: "Decision"},
{user_id: 2, event_id: 4, fighter_guess: "Rob Font", method_guess: "Submission"},
{user_id: 2, event_id: 5, fighter_guess: "Charles Oliveira", method_guess: "Decision"},
{user_id: 2, event_id: 5, fighter_guess: "Rose Namajunas", method_guess: "Submission"},
{user_id: 2, event_id: 6, fighter_guess: "Aleksandar Rakic", method_guess: "Decision"},
...
我对预测模型进行了验证,在允许进行预测之前检查相关 event_id 的状态是否为“即将到来”。因此,当我 运行 db:seed 时,它无法通过验证,因为某些事件不是“即将发生”。
事件结构
{event_name: "UFC FN: LEMOS x ANDRADE", f1: "Amanda Lemos", f2: "Jessica Andrade", f3: "Clay Guida", f4: "Claudio Puelles", status: "Concluded", date: "Sat, Apr 23"},
{event_name: "UFC FN: FONT x VERA", f1: "Rob Font", f2: "Marlon Vera", f3: "Andrei Arlovski", f4: "Jake Collier", status: "Concluded", date: "Sat, Apr 30"},
{event_name: "UFC 274", f1: "Charles Oliveira", f2: "Justin Gaethje", f3: "Rose Namajunas", f4: "Carla Esparza", status: "Upcoming", date: "Sat, May 7"},
Prediction.rb
class Prediction < ApplicationRecord
belongs_to :event
belongs_to :user
validate :prediction_no_longer_allowed
def prediction_no_longer_allowed
if self.event.concluded? || self.event.in_progress?
errors.add(:status, ": Event is Locked")
end
end
end
Event.rb
...
def concluded?
self.status == "Concluded"
end
def in_progress?
self.status == "In Progress"
end
对于我想要与其他人共享应用程序以测试功能的场景,如果我可以绕过验证以进行播种,将会节省大量时间。
我按以下顺序播种数据: 用户 --> 事件 --> 预测
你可以使用insert_all
方法
Inserts multiple records into the database in a single SQL INSERT statement. It does not instantiate any models nor does it trigger Active Record callbacks or validations. Though passed values go through Active Record's type casting and serialization.
Prediction.insert_all([
{user_id: 2, event_id: 3, fighter_guess: "Jessica Andrade", method_guess: "Decision"},
{user_id: 2, event_id: 3, fighter_guess: "Claudio Puelles", method_guess: "Submission"},
{user_id: 2, event_id: 2, fighter_guess: "Vicente Luque", method_guess: "Decision"},
{user_id: 2, event_id: 2, fighter_guess: "Caio Borralho", method_guess: "TKO/KO"},
{user_id: 2, event_id: 1, fighter_guess: "Korean Zombie", method_guess: "TKO/KO"},
{user_id: 2, event_id: 1, fighter_guess: "Petr Yan", method_guess: "Decision"},
{user_id: 2, event_id: 4, fighter_guess: "Rob Font", method_guess: "Submission"},
{user_id: 2, event_id: 5, fighter_guess: "Charles Oliveira", method_guess: "Decision"},
{user_id: 2, event_id: 5, fighter_guess: "Rose Namajunas", method_guess: "Submission"},
{user_id: 2, event_id: 6, fighter_guess: "Aleksandar Rakic", method_guess: "Decision"},
])
创建第一个特定事件和用户可能更好。然后调用user_id: my_super_user.id, event_id: my_super_event.id
等