开发者

ActiveRecord: get values in right order as in :select defined

I have a string with column names and I want to print out the keys and value开发者_Python百科s in this order.

# controller
fields = "name, year, title"
@blas = Bla.find(23, :select=>fields)

# view
<% @blas.attributes.each do |k,v| %>
  <%=k %>:<%=v %><br>
<% end %>

The result I get is in this order "name, title, year" but I want "name, year, title", like I defined it in fields. How can I do that?


You could cycle through them like this:

# controller
@fields = "name, year, title"
@blas = Bla.find(23, :select=>@fields)

# view
<% @fields.split(',').map(&:strip).each do |key| %>
  <%= k %>:<%= @blas[k] %><br />
<% end %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜