1

给定以下模型、关系和编辑表格

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

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

4

0 回答 0