Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

i'm in the middle of my upgrade and am running into some problems.

Here is my error:

/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, :dependent, :as, :through, :source, :source_type, :inverse_of, :counter_cache, :join_table (ArgumentError)

Does it have something to do with my scopes? For example:

scope :total_views, order('total_views DESC')

or

default_scope { order: :sort_order }

or

scope :recent, order: 'created_at desc'

I have bunch of scopes that use order, what is going on?

like image 234
TuxedoTomson Avatar asked Nov 01 '22 17:11

TuxedoTomson


1 Answers

Named scopes in Rails 4 take lambdas now instead of hashes. Within a lambda use the new query syntax and not the old hash syntax:

default_scope { order(:sort_order) } 

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

Read more about ActiveRecord querying: http://edgeguides.rubyonrails.org/active_record_querying.html#scopes

like image 153
spickermann Avatar answered Nov 10 '22 07:11

spickermann