Tag: routing error
(page 1 of 4)

Upgrading to Ruby on Rails 4.1 was much easier than moving from 3.2 to 4.0. Maybe because I try to keep all the apps up-2-date, maybe because Rails guys didn’t change much stuff ;) (or maybe both). Either way, lets get through it.

Paperclip – String based terminators are deprecated, please use a lambda

DEPRECATION WARNING: String based terminators are deprecated, please use a lambda.
(called from has_attached_file at app/config/initializers/paperclip_extensions.rb:22)

The ability to pass in strings as a class name to set_fixture_class
will be removed

Next deprecation warning:

DEPRECATION WARNING: The ability to pass in strings as a class name to `set_fixture_class`
will be removed in Rails 4.2. Use the class itself instead.
(called from block in initialize at gems/activerecord-4.1.0/lib/active_record/fixtures.rb:465)

It you use Rspec you probably won’t see this issue at all (or if you don’t use fixtures). One of my apps unfortunately still does. Solution to this is really simple. Instead of:

# This will throw an error
@articles = Article.limit(10).order('created_at DESC').reverse!

Instead you have to cast ActiveRecord::Relation to an array:

@articles = Article.limit(10).order('created_at DESC').to_a.reverse!

Keep in mind, that casting with to_a will deprive you from all benefits of lazy loading with Rails relations so use it carefully. But on the other hand, reverse! on ActiveRecord::Relation did the same, so if you used it and it was ok, than feel free ;)

Other issues

Well to be honest I didn’t have any more issues. I’ve decided to remove Squeel gem from all of my projects, since it is not currently maintained. Thanks to that I’ve finally got rid of this irritating deprecation warning:

DEPRECATION WARNING: Core extensions are deprecated and will be removed in Squeel 2.0.
(called from /app/config/initializers/squeel.rb:2:in `block in <top (required)>')

Summary

Rails 4.1 is not a big step, although it is a required one if you want to upgrade to 4.2 in the future. If you have decent test coverage level, you should not have big issues with this upgrade.

Assets – why aren’t they working in a proper way?

No non-fingerprinted asset files versions for you. I’ve noticed this issue in a really painful way: on the production. It isn’t documented anywhere, so I assume, that this is a bug (nasty one btw). When you perform:

rake assets:precompile

It generates whole bunch of files, however you might notice, that there’s no non-fingerprinted once there. All of them include fingerprints. I’ve even tried to disable fingerprinting at all with:

config.assets.digest = false

but Rails keeps ignoring that.

My first reaction after I’ve noticed that, was like that:

I do like digest idea, but there are some libraries (like Ckeditor) that won’t work without “clean” file versions. So until it is fixed, I’ll be using a simple rake task that I’ve created:

Load method

Load causes the records to be loaded from the database if they have not been loaded already. You can use this if for some reason you need to explicitly load some records before actually using them. The return value is the relation itself, not the records. You can treat this a bit as a replacement for all method.

None scopes

None scopes are one smart way to handle privileges management for AR resources (but not only for that!). Sometimes we want to create a method, that returns limited amount of objects based on incoming data. In previous Rails versions we would normally return an empty array if we wouldn’t have any privileges. However that might be troublesome when we’re chaining scopes:

protected attributes are out

Finally! I’ve never like this idea. It should not be a models responsibility to manage privileges. I will talk about that more in the next part but for know you need to know that Rails 4 moved the parameter sanitization from the model to the controller layer.

ordering for scopes works in a different way

I won’t even try to count how many times I had to create a scope that looked just like a different one but with a different order. Luckily it ends now! Rails 4 ordering changes the order order :-) Until now any new order has been appended as a last one. This caused troubles sometimes:

class Fancy < ActiveRecord::Base
# Let's assume that this is a scope that is used in many, many places
scope :active, ->{ where(active: true).order('created_at ASC') }
end

I would love to list all the active Fancy objects, but in a different order. I don’t want to change that scope, since it is widely used. So what can we do? Probably we would need to create a new similar scope. But not in Rails 4! In Rails 4 orders aren’t appended but instead they are prepended, so we can create scopes with default sort order but change it on demand:

First this might cause you a bit of troubles, especially if you’re using meta_search or Ransack. You’ll need to rewrite most of your search invocations. But in a longer perspective, this change is really good. If you want to pass multiply sorting orders, instead of doing something weird like that:

# This will generate sorting first by title and then by created at
Fancy.all.order('created_at DESC').order('title ASC')

You may want to use this syntax:

# Here order will be from left to right
Fancy.all.order('title ASC', 'created_at DESC')

regexp validation for validates_format_of

After trying to run your Rails app, you may see such an ArgumentError

ArgumentError: The provided regular expression is using multiline anchors (^ or $),
which may present a security risk. Did you mean to use \A and \z,
or forgot to add the :multiline => true option?

If you don’t expect multiline incoming data, you need to replace all the “^” with “\A” and all the “$” with “\z”. If you expect multiline strings, just specify the multiline: true option. After that, you’re ready to go.

auto_explain_threshold_in_seconds is gone

Just remove it from your config file and:

MySQL strict mode

Not sure if it came with Rails 4 – but I like that! If you encounter such an error:

ActionView::Template::Error: Mysql2::Error: Incorrect table name '': SHOW FULL FIELDS FROM ``

Didn’t figure that out yet. Unfortunately I had to do a workaround and I’ve extracted common functionalities into a module that is included in all the classes that were inheriting from my abstract class.