2013-03-02 152 views
2

假设我有一个Foo类belongs_to:bar。根据父母的职责排列的范围排序

酒吧有一个属性名称。我想创建一个通过bar.name命令Foos的范围。我如何用范围做到这一点?

赞, scope:by_name,order('')?

我对rails很新,所以我很抱歉如果这个问题没有意义。

回答

8

尝试......

scope :by_bar_name, order('bars.name').includes(:bar) 
+2

.joins(:巴)代替包含也是一种选择 – Swards 2013-03-03 00:31:21

0

或者更少的内存密集型:

scope :by_bar_name, joins(:bar).order('bars.name')