A copy of xxx has been removed from the module tree but is still active

Tenant is sort of a red herring – the error would occur if you referenced any bit of app that needs to be loaded by rails’ const_missing trick. The problem is that you are taking something reloadable (your module) and then including it in something not reloadable (ActiveRecord::Base or, in your earlier example ActionMailer::Base). At … Read more

How to solve error “Missing `secret_key_base` for ‘production’ environment” (Rails 4.1)

I had the same problem and solved it by creating an environment variable to be loaded every time I logged in to the production server, and made a mini-guide of the steps to configure it: I was using Rails 4.1 with Unicorn v4.8.2 and when I tried to deploy my application it didn’t start properly … Read more

What is `params.require(:person).permit(:name, :age)` doing in Rails 4?

The params in a controller looks like a Hash, but it’s actually an instance of ActionController::Parameters, which provides several methods such as require and permit. The require method ensures that a specific parameter is present, and if it’s not provided, the require method throws an error. It returns an instance of ActionController::Parameters for the key … Read more

Rails: validate uniqueness of two columns (together)

You can use a uniqueness validation with the scope option. Also, you should add a unique index to the DB to prevent new records from passing the validations when checked at the same time before being written: class AddUniqueIndexToReleases < ActiveRecord::Migration def change add_index :releases, [:country, :medium], unique: true end end class Release < ActiveRecord::Base … Read more

Determine what attributes were changed in Rails after_save callback?

Rails 5.1+ Use saved_change_to_published?: class SomeModel < ActiveRecord::Base after_update :send_notification_after_change def send_notification_after_change Notification.send(…) if (saved_change_to_published? && self.published == true) end end Or if you prefer, saved_change_to_attribute?(:published). Rails 3–5.1 Warning This approach works through Rails 5.1 (but is deprecated in 5.1 and has breaking changes in 5.2). You can read about the change in this pull … Read more

Rails 4 Authenticity Token

I think I just figured it out. I changed the (new) default protect_from_forgery with: :exception to protect_from_forgery with: :null_session as per the comment in ApplicationController. # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. You can see the difference by looking at the source for request_forgery_protecton.rb, … Read more

Rails: How to reference images in CSS within Rails 4

Sprockets together with Sass has some nifty helpers you can use to get the job done. Sprockets will only process these helpers if your stylesheet file extensions are either .css.scss or .css.sass. Image specific helper: background-image: image-url(“logo.png”) Agnostic helper: background-image: asset-url(“logo.png”, image) background-image: asset-url($asset, $asset-type) Or if you want to embed the image data in … Read more

Paperclip::Errors::MissingRequiredValidatorError with Rails 4

Starting with Paperclip version 4.0, all attachments are required to include a content_type validation, a file_name validation, or to explicitly state that they’re not going to have either. Paperclip raises Paperclip::Errors::MissingRequiredValidatorError error if you do not do any of this. In your case, you can add any of the following line to your Post model, … Read more

Auto-loading lib files in Rails 4

I think this may solve your problem: in config/application.rb: config.autoload_paths << Rails.root.join(‘lib’) and keep the right naming convention in lib. in lib/foo.rb: class Foo end in lib/foo/bar.rb: class Foo::Bar end if you really wanna do some monkey patches in file like lib/extensions.rb, you may manually require it: in config/initializers/require.rb: require “#{Rails.root}/lib/extensions” P.S. Rails 3 Autoload … Read more

How is attr_accessible used in Rails 4?

Rails 4 now uses strong parameters. Protecting attributes is now done in the controller. This is an example: class PeopleController < ApplicationController def create Person.create(person_params) end private def person_params params.require(:person).permit(:name, :age) end end No need to set attr_accessible in the model anymore. Dealing with accepts_nested_attributes_for In order to use accepts_nested_attribute_for with strong parameters, you will … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)