Simple way of turning off observers during rake task?
You could add an accessor to your user model, something like "skip_activation" that wouldn't need to be saved, but would persist through the session, and then check the flag in the observer. Something like
class User
attr_accessor :skip_activation
#whatever
end
Then, in the observer:
def after_save(user)
return if user.skip_activation
#rest of stuff to send email
end
Simple way of turning off observers during rake task?
You could add an accessor to your user model, something like "skip_activation" that wouldn't need to be saved, but would persist through the session, and then check the flag in the observer. Something like
class User
attr_accessor :skip_activation
#whatever
end
Then, in the observer:
def after_save(user)
return if user.skip_activation
#rest of stuff to send email
end
Rails Observers, Plugins and Migrations are in a race, who wins?
I haven't found a decent way to disable Observers at run time. This has been previously discussed in Simple way of turning off observers during rake task?
However, I guess you could "unplug" the plugin code from your model by redefining the troublesome model in your migration:
class YourMigration < ActiveRecord::Migraation
class YourModel < ActiveRecord::Base; end
def self.up
...
end
def self.down
...
end
end
Rake task failing: Environment not loading
I found a solution that seems to work for me. I found...
Simple way of turning off observers during rake task?
I used...
Rails.configuration.active_record.observers = []
...in my rake task to disable observers. So my complete rake task now looks like...
task :send_reminder_emails => :environment do
Rails.configuration.active_record.observers = []
Registration.send_reminder_emails
end
...I just run bundle exec rake send_reminder_emails
to run the rake task...
Execute the rake task for longer data
Do you have any sort of background job gem that you are using (eg. delayed_job)? If so, I would have a rake task that creates a bunch of background jobs to do the work.
If you have an array you are iterating through, you could do it like:
items.in_groups_of(10000, false) do |batch|
# create background job for this batch
end
If it's a collection, you can use something like:
@items.find_in_batches(batch_size: 10000) do |batch|
# create background job for this batch
end
How do I disable cache sweepers for testing purposes
I was able to use the no-peeping-toms gem to block observers during testing.
NOTE: This also works for rake tasks. I had an issue with observers being called during a migration and this solved it.
Related Topics
Escaping Single and Double Quotes in a String in Ruby
Openssl::Ssl::Sslerror: Ssl_Connect Returned=1 Errno=0 State=Unknown State: Unknown Protocol
Generate a Powerset of a Set Without Keeping a Stack in Erlang or Ruby
How to Invoke an Instance Method on a Ruby Module Without Including It
Rails: Should Partials Be Aware of Instance Variables
Error Installing Nokogiri: Failed to Build Gem Native Extension & Libiconv Is Missing (Osx)
Create_Or_Update Method in Rails
Digital Signature Verification with Openssl
How to Configure Ruby on Rails with Oracle
Yaml How Many Spaces Per Indent
Prevent Rails Test from Deleting Seed Data
Add "# Coding: Utf-8" to All Files
Portable Ruby on Rails Environment
Ruby on Rails Scalability/Performance
Ruby on Rails Plural (Controller) and Singular (Model) Convention - Explanation