Как лучше запускать запросы на удаление в Rails Migration - PullRequest
1 голос
/ 28 июня 2010

Я новичок в Rails и хочу написать несколько запросов self.down.В настоящее время я жестко запрограммировал весь запрос.Есть ли более простой способ сделать это?

  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

Спасибо

Ответы [ 2 ]

2 голосов
/ 28 июня 2010

Вы можете сделать это в стиле 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
1 голос
/ 28 июня 2010

Согласно @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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...