用于关联的Rails'order'(Mysql2 ::错误:未知列)

时间:2016-07-19 08:04:04

标签: ruby-on-rails activerecord

我尝试按照这样的方式订购图书:books.includes(:library).order("libraries.name"),但出现了这样的错误:ERROR -- : Mysql2::Error: Unknown column 'libraries.name' in 'order clause'...

book.rb:

belongs_to :library, class_name: "Bibliotheca", foreign_key: :bibliotheca_id

bibliotheca.rb

has_many :books, class_name: "Book", foreign_key: :bibliotheca_id

1 个答案:

答案 0 :(得分:2)

您很可能需要将order中的表名更改为

order('bibliothecas.name')

当您将字符串或哈希参数传递给AR查询时,您需要实际的表名而不是AR别名。