Uprgrading to Rails 4.1 getting error: ArgumentError: Unknown key :: order

I am in the middle of my update and am facing some problems.

Here is my mistake:

/Users/jay/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activesupport-4.1.6/lib/active_support/core_ext/hash/keys.rb:71:in `block in assert_valid_keys': Unknown key :: order. Valid keys are :: class_name ,: class ,: foreign_key ,: validate ,: autosave ,: table_name ,: before_add ,: after_add ,: before_remove ,: after_remove ,: extend ,: primary_key ,: depend ,: as ,: through ,: source ,: source_type ,: inverse_of ,: counter_cache ,: join_table (ArgumentError)

Is this something related to my areas? For instance:

scope :total_views, order('total_views DESC')

or

default_scope { order: :sort_order }

or

scope :recent, order: 'created_at desc'

I have many areas that use order, what happens?

+5
source share
3 answers

Named scopes in Rails 4 now take lambdas instead of hashes. Inside lambda, use the new request syntax, not the old hash syntax:

 default_scope { order(:sort_order) } scope :total_views, -> { order('total_views DESC') } scope :recent, -> { order('created_at DESC') } 

More about ActiveRecord request: http://edgeguides.rubyonrails.org/active_record_querying.html#scopes

+2
source

Your areas should be in lambda shape.

scope :total_views, -> { order('total_views DESC') }

default_scope -> { order: :sort_order }

scope :recent, -> { order: 'created_at desc' }

0
source

I updated act_as_tree to version 2.1.0 and the problem disappeared.

0
source

Source: https://habr.com/ru/post/1202458/


All Articles