Ruby on rails 在Rails迁移中触发删除查询的更好方法是什么
我是Rails的新手,我想写一些自我记录查询。目前我正在对整个查询进行硬编码。有更简单的方法吗Ruby on rails 在Rails迁移中触发删除查询的更好方法是什么,ruby-on-rails,migration,Ruby On Rails,Migration,我是Rails的新手,我想写一些自我记录查询。目前我正在对整个查询进行硬编码。有更简单的方法吗 def self.down delete("delete from trigger_events where name = 'CHANGE_APPROVED'") delete("delete from notifications where trigger_event_id = 5") delete("delete from notification_recipients
def self.down
delete("delete from trigger_events where name = 'CHANGE_APPROVED'")
delete("delete from notifications where trigger_event_id = 5")
delete("delete from notification_recipients where notification_id = 5")
delete("delete from n_m_d where notification_id = 5 and msg_index=15")
end
谢谢您可以用ActiveRecord风格来完成:
def self.down
TriggerEvents.find_by_name('CHANGE_APPROVED').destroy_all
Notifications.find_by_trigger_event_id(5).destroy_all
NotificationRecipients.find_by_notification_id(5).destroy_all
NMD.find_by_notification_id_and_msg_index(5,15).destroy_all
end
根据@shingara,但经过修改以处理未发现的病例
def self.down
TriggerEvents.find_all_by_name('CHANGE_APPROVED').map(&:destroy)
Notifications.find_all_by_trigger_event_id(5).map(&:destroy)
NotificationRecipients.find_all_by_notification_id(5).map(&:destroy)
NMD.find_all_by_notification_id_and_msg_index(5,15).map(&:destroy)
end
我尝试了您的解决方案,但当查询的值为零时出现异常。如何干净利落地完成?感谢您提供的未找到的案例解决方案!!!!这正是我要找的!