自定义顺序忽略SQL ORDER BY代码

时间:2017-05-29 10:59:07

标签: wice-grid

我正在尝试使用custom_order来呈现任意初始订单。

# GET /sequences
def index
@sequences_grid = initialize_grid(Sequence,
  enable_export_to_csv: false,
              per_page: 10,
          custom_order: {
   'sequences.username' => "CASE WHEN username LIKE '#{current_user.username}' THEN 0 ELSE 1 END, username"
                        },
                  name: 'seq_g1')
end

我们的想法是让当前用户的记录渗透到列表的顶部。

用户指南指出“键是数据库列的完全限定名,并且在ORDER BY子句中使用所需的SQL块”。

在数据库编辑器中运行时,SQL代码按预期工作,但网格未显示所需的顺序。

日志在任何wice_grid问题上都是静默的,报告的请求是:

10:10:35 web.1  |   Sequence Load (0.3ms)  SELECT distinct username FROM "sequences"  ORDER BY username asc
10:10:35 web.1  |   Sequence Load (0.3ms)  SELECT distinct classtype FROM "sequences"  ORDER BY classtype asc
10:10:35 web.1  |   Sequence Load (0.2ms)  SELECT distinct description FROM "sequences"  ORDER BY description asc
10:10:35 web.1  |   Sequence Load (0.2ms)  SELECT distinct sequencenumber FROM "sequences"  ORDER BY sequencenumber asc
10:10:35 web.1  |   Sequence Load (0.2ms)  SELECT distinct target FROM "sequences"  ORDER BY target asc
10:10:35 web.1  |   Sequence Load (0.2ms)  SELECT distinct indicator FROM "sequences"  ORDER BY indicator asc

我怀疑SQL代码可能会以某种方式被破坏,但在我深入研究之前,我认为有人可能会遇到类似的问题?

此致 汤姆。

1 个答案:

答案 0 :(得分:0)

经过一段时间的讨论后,我发现SQL没问题,但你需要指定顺序以及custom_order。

用户指南提供的示例似乎有点误导......

@hosts_grid = initialize_grid(Host,
  custom_order: {
 'hosts.ip_address' => 'INET_ATON(hosts.ip_address)'
                })

但是这些例子提供了......

@status_grid1 = initialize_grid(Status,
  order: 'statuses.name',
  custom_order: {
    'statuses.name' => 'length( ? )'
  }
)

所以以下做了我需要的......

# GET /sequences
def index
@sequences_grid = initialize_grid(Sequence,
  enable_export_to_csv: false,
              per_page: 10,
                 order: 'sequences.username',
          custom_order: { 'sequences.username' => "CASE WHEN sequences.username LIKE '#{current_user.username}' THEN 0 ELSE 1 END, username, updated_at" },
       order_direction: 'desc',  
                  name: 'seq_g1')
end

...即它创建了一个列表,其中当前用户的记录首先按updated_at的降序排列,然后是其他用户的记录。