1
假设我有以下型号:ActiveRecord的查询订购
class Car < ActiveRecord::Base
belongs_to :seat
...
end
class Seat < ActiveRecord::Base
belongs_to :color
...
end
class Color < ActiveRecord::Base
attr_reader :name
...
end
如果我得到汽车的名单,我想顺序由汽车color.name,怎么写的顺序查询?
class Car < ActiveRecord::Base
belongs_to :seat
...
def cars_order_by_color(car_ids)
where(:id=>car_ids).order(?????) #HOW TO ORDER BY COLOR.name
end
end
嗨,哈利喜悦,感谢您的回复,但你的答案是不是有什么我期望,我知道如何订购。如果仔细检查我的模型,我的问题是关于通过关联的模型对象进行排序。我想订购彩色汽车,其中“颜色”是与“座椅”相关的型号,“座椅”是与“汽车”相关的型号。这就是我想通过color.name订购汽车,这种订单,我不知道如何实施。 – Mellon 2011-01-25 13:37:54