Rails通过关联排序

时间:2016-02-10 23:42:47

标签: ruby-on-rails ruby

我正在尝试从Members

升序的模型Member.board.order进行排序

在我的控制器上,institutional_controller.rb我有:

@display = Member.includes(:board).where('is_board = ?', true).order('member.board.order ASC').references(:board)

# board.rb and member.rb

class Board < ActiveRecord::Base
  belongs_to :member
end

class Member < ActiveRecord::Base
  has_one :board
end


# index.html.erb

<% @display.each do |member|  %> [...]

我得到的错误是:

PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "board"
LINE 1: ...= "members"."id" WHERE (is_board = 't')  ORDER BY member.board [...]

谢谢。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

它现在可以这样工作:

Member.includes(:board).where('is_board = ?', true).order('boards.order ASC').references(:board)

答案 1 :(得分:1)

这应该可以解决问题;

@display = Member.joins(:board).where(is_board: true).order('boards.order ASC')

你也应该使用;

render 'partial', collection: @display

在您看来,因为它更有效率和可读性。