Rails order by in associated model

后端 未结 5 1433
醉酒成梦
醉酒成梦 2020-12-14 06:01

I have two models in a has_many relationship such that Log has_many Items. Rails then nicely sets up things like: some_log.items which returns all of the associ

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 06:52

    rails 4.2.20 syntax requires calling with a block:

    class Item < ActiveRecord::Base
      default_scope { order('some_col DESC') }
    end
    

    This can also be written with an alternate syntax:

    default_scope { order(some_col: :desc) }
    

提交回复
热议问题