Rails: order using a has_many/belongs_to relationship

后端 未结 2 405
谎友^
谎友^ 2021-02-07 07:12

I was wondering if it was possible to use the find method to order the results based on a class\'s has_many relationship with another class. e.g.

# has the colum         


        
2条回答
  •  不思量自难忘°
    2021-02-07 07:52

    You need to join the related table to the request.

    @result = DogTag.find(:all, :joins => :dog, :order => 'dogs.name')
    

    Note that dogs is plural in the :order statement.

提交回复
热议问题