如何在使用 rails6 中的连接执行基于转换表字段的排序时获取当前区域设置数据而不是全部

问题描述

我需要根据 product_statuses 表的 translations 表的 'name' 字段的升序/降序对产品进行排序。

在控制器中,我使用:

products = Product.where(id: pr_ids).ordered_status(sort_order)

product_status.rb

has_many :products

product.rb

belongs_to :status,class_name: "ProductStatus",foreign_key: "status_id",inverse_of: :products,optional: true

product_status_translations 表详情如下:

         Column              |              Type             

 id                              | bigint                        
 product_status_id               | integer                       
 locale                          | character varying             
 created_at                      | timestamp(6) without time zone
 updated_at                      | timestamp(6) without time zone
 name                            | character varying             

在 products.rb 中,我写了一个范围来按名称顺序获取产品:

scope :ordered_status,-> (sort_order) {joins(status: :translations).order("name    "+sort_order)}

我正在按顺序获取产品,但考虑到所有语言环境,正在获取多条记录。不仅获取当前用户语言环境,还获取所有语言环境。因此,产品结果记录会出现重复。

请帮忙

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)