0

现在我有这样的模型:

class CrossList < ActiveRecord::Base
  belongs_to :price_list  
end

class PriceList < ActiveRecord::Base
  has_many :cross_lists
end

我在方法中有这样的代码:

@search_res = CrossList.search(search_condition)
@prlist = PriceList.find(:first, :conditions => { :id => @search_res.map(&:price_list_id)}, :order => 'price desc')

但是我如何使用模型关联来做到这一点?我会写

@prlist = @search_res.price_lists

但这对吗?但它也必须像查找一样进行排序,我该怎么做?

还有什么是最好的?查找,或关联?

(rails中的新手)

4

1 回答 1

1

You can set the order on association with :order, like so:

class Company < ActiveRecord::Base
   has_many :employees, order: "age DESC"
end

Employees will be ordered by default:

Company.all.sample.employees

Yes, use associations in this scenario.

于 2013-06-23T15:22:51.913 回答