Rails命令column_names的集合

时间:2014-09-26 14:21:57

标签: ruby-on-rails

我正在使用表单中的Rails表中的column_names

column_names()public 将列名称数组作为字符串返回。

如何订购收藏清单?

这不起作用:

<%= f.input :field_name, :label => "Field Name", :collection => Vehicle.column_names.order(:name) %>

我明白了:

undefined method `order' for #<Array

这不起作用:

<%= f.input :field_name, :label => "Field Name", :collection => Vehicle.column_names.sort_by{|e| e[:name]} %>

我明白了:

no implicit conversion of Symbol into Integer

2 个答案:

答案 0 :(得分:1)

Model.column_names 返回一个列名数组。

例如

Loading development environment (Rails 3.2.13)
#Customer is my rails model
2.1.2 :001 > Customer.column_names
 => ["id", "customer_name", "customer_tier", "customer_channel", "created_at", "updated_at"] 

#this sorts it
2.1.2 :002 > Customer.column_names.sort
 => ["created_at", "customer_channel", "customer_name", "customer_tier", "id", "updated_at"] 

答案 1 :(得分:0)

<%= f.input :field_name, :label => "Field Name", :collection => Vehicle.column_names.sort{|a,b| a[:name] <=> b[:name]} %>

不确定您的数据收集情况如何,但我确信这应该有用。

干杯!