按关联排序的列适用于MySQL但不适用于Postgresql

时间:2016-06-15 08:05:28

标签: ruby-on-rails postgresql activerecord

我有那些典型的 class VisibilityColumn : IValueConverter { public CollectionViewSource CurrentCollection { get; set; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { List<string> castedCollection = (List<string>)CurrentCollection.Source; bool result=castedCollection.Exists(x => x == (string)parameter); if (result == true) return Visibility.Visible; else return Visibility.Collapsed; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new Exception("The method or operation is not implemented."); } } OrderLineItem

ProductOrder个,LineItemLineItem个字段。

我正在尝试根据名为product_id的产品表的列对LineItem中的Order进行排序。

所以,这在本地工作,在MySQL数据库上,但不在生产中,我使用PostgreSQL。

code

错误:

scope :sort_by_product_id, -> {
  includes(:product)
  .order("products.code asc")
}

我尝试使用ActionView::Template::Error (PG::SyntaxError: ERROR: syntax error at or near "products" LINE 1: ..._id" WHERE "line_items"."order_id" = $1 ORDER BY products... ^ : SELECT "line_items".* FROM "line_items" INNER JOIN "products" ON "products"."id" = "line_items"."product_id" WHERE "line_items"."order_id" = $1 ORDER BY products.code asc): 代替joins,但在生产中仍然存在相同的错误。

2 个答案:

答案 0 :(得分:1)

scope :sort_by_product_id, -> {
  joins(:product)
  .merge(Product.order(code: :asc))
}

答案 1 :(得分:0)

scope :sort_by_product_id, -> do
  includes(:product).order(products: { code: :asc })
end