小编Jef*_*her的帖子

如何删除嵌套 has_many 关联中关联的所有对象?

我的模型是:

class Campaign < ActiveRecord::Base
  has_many :days, dependent: :destroy
end

class Day < ActiveRecord::Base
  belongs_to :campaign

  has_many :time_slots
  before_destroy { time_slots.destroy_all }
end

class TimeSlot < ActiveRecord::Base
  belongs_to :day
  has_and_belongs_to_many :users
end
Run Code Online (Sandbox Code Playgroud)

我希望能够删除一个活动并删除其所有关联的天数和时间段。我还想删除 time_slot_users 连接表中的记录。

我尝试使用dependent: :destroy,但它似乎没有级联?我应该使用before_destroy回调吗?

destroy和 和有destroy_all什么区别?我已阅读:http : //api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Delete+or+destroy%3F并且差异仍然模糊。

ruby-on-rails ruby-on-rails-3

5
推荐指数
1
解决办法
3179
查看次数

为什么不使用 ActiveJob 发送电子邮件?

我正在将应用程序升级到 Rails 4.2,以便我们可以利用 ActionMailer 的deliver_later方法。一切都在发展中运作良好。当我使用capistrano-sidekiqgem部署到我们的临时服务器时,似乎没有发送电子邮件。根据此日志,该作业似乎已正确排队并执行。

服务器是 Ubuntu 14.04,通过 apt-get 安装了 redis。

[ActiveJob] Enqueued ActionMailer::DeliveryJob (Job ID: 51ddade2-4689-40fd-aeda-7e94f7260e43) to Sidekiq(mailers) with arguments: 
"ApplicationMailer", "admin_message", "deliver_now", "Broken Link", "Testing an email from staging."
[ActiveJob] [ActionMailer::DeliveryJob] [51ddade2-4689-40fd-aeda-7e94f7260e43] Performing ActionMailer::DeliveryJob from Sidekiq(mailers) 
with arguments: "ApplicationMailer", "admin_message", "deliver_now", "Broken Link", "Testing an email from staging."
[ActiveJob] [ActionMailer::DeliveryJob] [51ddade2-4689-40fd-aeda-7e94f7260e43]   Rendered application_mailer/admin_message.html.erb within  layouts/mailer (1.9ms)
[ActiveJob] [ActionMailer::DeliveryJob] [51ddade2-4689-40fd-aeda-7e94f7260e43]   Rendered layouts/mailer.html.erb (8.8ms)
[ActiveJob] [ActionMailer::DeliveryJob] [51ddade2-4689-40fd-aeda-7e94f7260e43]   Rendered application_mailer/admin_message.text.erb within layouts/mailer (0.8ms)
[ActiveJob] [ActionMailer::DeliveryJob] [51ddade2-4689-40fd-aeda-7e94f7260e43]   Rendered …
Run Code Online (Sandbox Code Playgroud)

capistrano ruby-on-rails sidekiq

5
推荐指数
1
解决办法
2388
查看次数

ifstream变量可以是全局变量吗?

// stream from file.
ifstream file;

int main (int argc, char * argv[]) {

// get argument passed from command line
// This is file name
if (argc != 2 ) {
    cout << "use:  ./executable <filename>";

}else {
    //cout << "You are using filename: " << argv[1];

    // start the file stream
    file (argv[1]);
}
Run Code Online (Sandbox Code Playgroud)

有什么理由file(argv[1])会给出错误吗?我可以将ifstream作为全局变量吗?

c++ ifstream

2
推荐指数
1
解决办法
4620
查看次数