如何在编辑表单中对has_many进行排序?

时间:2012-08-10 05:02:29

标签: ruby-on-rails forms sorting has-many-through

鉴于以下模型,关系&编辑表格

class User < ActiveRecord::Base

  has_many :relationships, foreign_key: "follower_id", dependent: :destroy
  has_many :followed_users, through: :relationships, source: :followed
  has_many :reverse_relationships, foreign_key: "followed_id",
                               class_name: "Relationship",
                               dependent: :destroy
  has_many :followers, through: :reverse_relationships, source: :follower

end

<%= form_for(@topic) do |f| %>
<%= render 'shared/error_messages', object: f.object %>

    <%= f.label :name %>
    <%= f.text_field :name %>

    <%= f.label :Followers %>
    <%= f.fields_for :relationships do |builder| %>
        <%= f.collection_select :follower_id, User.all( order: "name"), :id, :name, include_blank: true %>
    <% end %>


    <%= f.submit "Save Changes" %></div>
<% end %>

class Relationship < ActiveRecord::Base
  attr_accessible :followed_id

  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"

  validates :follower_id, presence: true
  validates :followed_id, presence: true
end

在这种情况下,关系字段显示在给定用户的编辑表单中。因此,他们可以管理所有的粉丝。但问题是,在编辑页面上,用户按照添加顺序显示。有没有办法对它们进行排序,使它们按名称按字母顺序排列,尽管事物是:关系关联只有一个id?

0 个答案:

没有答案