如何使用转换文件对列进行排序

时间:2013-12-09 11:14:49

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2 ruby-on-rails-3.1

我的数据库中有两个表。 Eu tenho duas tabelas no banco de dados。 1ºtest_case_statuses 2ºtest_case_runs

第一个表包含列:id和identifier。 这个表的内容是这样的: id标识符 1新 2批准 3责备

第二个表有列id e status_id。

我的应用程序是葡萄牙语,所以我使用的是翻译文件。 该文件包含以下行:

labels
   ...
   test_cases_statuses:
     new: "Novo"
     approved: "Aprovado"
  reproved: "Reprovado"
...

问题是我需要按字母顺序对表的内容进行排序。 对于其他列,我使用以下代码:

查看/ test_plan_cases

...

<%= sortable_th @search, :test_case_number, {}, :remote => true %>
...

应用程序/助手/

def sortable_th(builder = nil, attribute = nil, *args) 

# th_classes = ['green']
th_classes = []
content = nil

unless builder.nil? || attribute.nil?
  th_classes << 'header'
  content = sort_link(builder, attribute, *args)

  if content.include? 'sort_link asc'
    th_classes << 'headerSortDown'
  elsif content.include? 'sort_link desc'
    th_classes << 'headerSortUp'
  end
end

th_classes = th_classes.join(' ')
content_tag :th, content, class: th_classes
 end



def sortable_teste(builder = nil, attribute = nil, *args) 
# th_classes = ['green']
th_classes = []
content = nil

unless builder.nil? || attribute.nil?
  th_classes << 'header'
  content = sort_teste(builder, attribute, *args)

  if content.include? 'sort_teste asc'
    th_classes << 'headerSortDown'
  elsif content.include? 'sort_teste desc'
    th_classes << 'headerSortUp'
  end
end

th_classes = th_classes.join(' ')
content_tag :th, content, class: th_classes

 end

控制器/ test_case_runs_controller

  @search = @project.test_plan_cases.allowed_for(@release, current_user).search search_values

需要指导和帮助才能做到这一点。 我正在考虑做的是使用枚举器类型来保存翻译文件的内容。 看起来像这样:

[1'Novo',

2'Aprovado',

3'Reprovado']

之后我迷失了自己,应该创建另一种形式的订购还是想调整上面的代码? 我感谢任何帮助=]

1 个答案:

答案 0 :(得分:0)

一种方法是对使用SQL计算的虚拟字段进行排序。您必须以这种方式构建字段定义:

CASE status_id
 WHEN 1 THEN ‘Novo′
 WHEN 2 THEN ‘Aprovado′
 WHEN 3 THEN ‘Reprovado′
END

然后您可以将其用作订单子句:

@search = @project.test_plan_cases.some_other_scope.order("#{case_above} ASC")

和类似的查询。