【问题标题】:Set default order of a has_many resource at the Active Record level [duplicate]在 Active Record 级别设置 has_many 资源的默认顺序 [重复]
【发布时间】:2016-07-13 07:25:26
【问题描述】:

如何设置从数据库中出来的资源的默认顺序。 比如:

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

有一些业务逻辑需要首先检查最近的日期。

当前代码导致此错误。

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):

正在尝试this,因为它收到了很好的评价(6 年前)。我是否遗漏了一些简单的东西,或者为了今天更好的方式而贬低了它?

【问题讨论】:

  • 您使用的是哪个版本的导轨?
  • @Pavan Rails 4.2.5

标签: ruby-on-rails


【解决方案1】:
has_many :trips, -> { order(departure_date: :desc) }

Scroll down to "scopes"

【讨论】:

  • 我以前没用过`->`。你能解释一下它在做什么吗?
  • 添加了文档链接!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-13
  • 2017-07-31
  • 2013-02-22
  • 1970-01-01
  • 2017-06-03
  • 1970-01-01
相关资源
最近更新 更多