ActiveRecord包括订单

时间:2012-11-07 19:00:55

标签: ruby-on-rails activerecord

我有模特:

 class FirstModel < AR::B
   belongs_to :second_model
 end

 class SecondModel < AR::B
   #name
   has_many :first_model
 end

我有代码:

 @a = FirstModel.where(#some query)
 @a.include([:second_model])

我如何按名称包含SecondModel?

感谢。

1 个答案:

答案 0 :(得分:3)

您可能在has_many方法中要求order选项。例如:

has_many :first_models, order: 'name'

但是您的代码段并不完美,因为FirstModel只有一个SecondModel(属于),您可能会要求SecondModel.where(...).include(:first_models)