【发布时间】:2013-12-02 21:16:20
【问题描述】:
假设我在 rails4 应用程序中有以下内容:
class Foo < AR::Base
has_many :foo_bar
has_many :bar, through: :foo_bar
end
并且 foo_bar 表有一个带有数字的“排序”列。为了让事情变得更有趣,“条形”表也有一个带有数字的“排序”列。与连接表排序列同名。而且,这很重要...
class Bar < AR::Base
default_scope -> { order(:sort) }
end
我需要在关联上有一个默认范围,例如:
foo = Foo.last
foo.bars # => List of bars, ordered by the number on the foo_bar table
我该怎么做?这意味着取消栏的范围,但只是排序
【问题讨论】:
标签: ruby-on-rails activerecord ruby-on-rails-4 associations has-many-through