Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default order of a has_many resource at the Active Record level [duplicate]

How do you set the default order of resources coming out of the db. Something like:

has_many :trips, :order => 'departure_date DESC'

There is some business logic that needs the most recent date being checked first.

Current code results in this error.

ArgumentError (Unknown key: :order. Valid keys are: :class_name,     :anonymous_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, :foreign_type):

Was trying this as it received good reviews (6yrs ago.) Am I missing something simple or has that be depricated in favor of a better way today?

like image 931
CheeseFry Avatar asked Mar 25 '16 14:03

CheeseFry


1 Answers

has_many :trips, -> { order(departure_date: :desc) }

Scroll down to "scopes"

like image 178
Matt Avatar answered Sep 21 '22 16:09

Matt