1

我有模型:

 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?

谢谢。

4

1 回答 1

3

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

has_many :first_models, order: 'name'

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

于 2012-11-07T19:26:33.610 回答