How do I force RAILS_ENV in a rake task?

For this particular task, you only need to change the DB connection, so as Adam pointed out, you can do this: namespace :db do namespace :test do task :reset do ActiveRecord::Base.establish_connection(‘test’) Rake::Task[‘db:drop’].invoke Rake::Task[‘db:create’].invoke Rake::Task[‘db:migrate’].invoke ActiveRecord::Base.establish_connection(ENV[‘RAILS_ENV’]) #Make sure you don’t have side-effects! end end end If your task is more complicated, and you need other aspects … Read more

How to skip ActiveRecord callbacks? [duplicate]

For Rails 3, ActiveSupport::Callbacks gives you the necessary control. I was just facing the same challenge in a data integration scenario where normally-desirable-callbacks needed to be brushed aside. You can reset_callbacks en-masse, or use skip_callback to disable judiciously, like this: Vote.skip_callback(:save, :after, :add_points_to_user) ..after which you can operate on Vote instances with :add_points_to_user inhibited

How do I ‘validate’ on destroy in rails

You can raise an exception which you then catch. Rails wraps deletes in a transaction, which helps matters. For example: class Booking < ActiveRecord::Base has_many :booking_payments …. def destroy raise “Cannot delete booking with payments” unless booking_payments.count == 0 # … ok, go ahead and destroy super end end Alternatively you can use the before_destroy … Read more

Error launching Rails server: undefined method ‘configure’

I resolved it by doing following step. Step 1: go to Project_Root_Directory/config/environment/development.rb Change this line Rails.application.configure do To Your_Rails_Application_Folder_name::Application.configure do For example my rails project folder name is ‘Spree_demo’ so Your_Rails_Application_Folder_name in the following line: Your_Rails_Application_Folder_name::Application.configure do will be replaced as SpreeDemo::Application.configure do Note: See underscore in your application folder name it gets removed. Hope … Read more

Rails 4: organize rails models in sub path without namespacing models?

By default, Rails doesn’t add subfolders of the models directory to the autoload path. Which is why it can only find namespaced models — the namespace illuminates the subdirectory to look in. To add all subfolders of app/models to the autoload path, add the following to config/application.rb: config.autoload_paths += Dir[Rails.root.join(“app”, “models”, “{*/}”)] Or, if you … Read more