Tag: Rails4

Rails 4.0.1: Revert change on ActiveRecord::Relation#order method monkey patch to keep Rails 4.0.0 order behaviour

It is really good habit to review source code of each new Rails release (or at least a changelog file). Today while reviewing this release note, I've noticed, that the Rails team is going to revert the ActiveRecord#order functionality, so it will work like in the 3.2 version.

I must say, that I'm a bit disappointed. I really got used to this functionality and I used it really often. It was quite convenient to create scopes with default sorting, that could be easily "overwritten" by any other. Of course after that change I can still use the reorder method, to get exactly same effect, but it will require a lot of changes in the code. Also IMHO it seems kinda unfair - I put a lot of effort to migrate from Rails 3.2 to Rails 4.0.0 (stable) and it seems, that some of that work just got wasted, because Rails guys seem to be a bit unstable. I can understand behaviour changes between major releases, but this is just a fuck*ng small one!

105012-you-shall-deal-with-it

If you're not willing to spend a lot of time getting back to previous ordering mode (and dealing with it), you can use this monkey patch (put it into config/initializers) to keep the current (4.0.0) ordering behaviour:

module ActiveRecord
  class Relation

    def order!(*args)
      args.flatten!
      validate_order_args args

      references = args.reject { |arg| Arel::Node === arg }
      references.map! { |arg| arg =~ /^([a-zA-Z]\w*)\.(\w+)/ && $1 }.compact!
      references!(references) if references.any?

      # if a symbol is given we prepend the quoted table name
      args = args.map { |arg|
        arg.is_a?(Symbol) ? "#{quoted_table_name}.#{arg} ASC" : arg
      }
      self.order_values = args + self.order_values
      self
    end

  end
end

Rafaels says that 4.0.1 ordering will stay as a default one, although I would not recommend doing any hasty moves now. I think it's worth waiting at least few months to find out, if they are not going to change it again soon.

Meanwhile you can review Github commit with this change. Also be prepared for the shitstorm that is coming on Thursday (4.0.1 release day)...

687474703a2f2f692e716b6d652e6d652f3335747837352e6a7067

Rails 4.0: ActionView::Template::Error: undefined method paginate with Kaminari

Refactoring can be sometimes a pain in the ass. Especially when you're refactoring things that are really old. Today I was cleaning an almost 5 years old piece of code. It was a Rails 4.0 (migrated many times from 1.2) app that had helpers auto-loading turned on. I hated this, because it forced us to use names with prefixes for many methods.

Disabling it was really easy (in config/application.rb):

config.action_controller.include_all_helpers = false

Of course after that you need to require all the helpers that you use:

helper ApplicationHelper
helper FormHelper

Everything was fine except one small detail:

ActionView::Template::Error: undefined method `paginate' for #<#<Class:0x0000000929a3c0>:0x0000000aa2c5b0>
    app/views/admin/module/urls/index.html.haml:1:in `_app_views_admin_module_urls_index_html_haml__883223636925488268_77061720'
    app/controllers/application_controller.rb:57:in `respond_with'
    app/controllers/admin/module/urls_controller.rb:9:in `index'
    test/controllers/admin/module/urls_controller_test.rb:103:in `block in <class:UrlsControllerTest>'

Kaminari stopped working. It seems that it was loaded without issues, except the helper. To fix this, just include in you application controller following code:

helper KaminariHelper

Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑