У меня есть две модели с отношением has_many к has_many через таблицу соединений.
class Article < ActiveRecord::Base
has_many :authorings, -> { order(:position) }, :dependent => :destroy
has_many :authors, through: :authorings
end
class Author < ActiveRecord::Base
has_many :authorings, -> { order(:position) }
has_many :articles, through: :authorings
end
class Authoring < ActiveRecord::Base
belongs_to :author
belongs_to :article
acts_as_list :scope => :author
end
Методы получения и установки для массива
def author_list
self.authors.collect do |author|
author.name
end.join(', ')
end
def author_list=(author_array)
author_names = author_array.collect { |i|
i.strip.split.each do |j|
j.capitalize
end.join(' ') }.uniq
new_or_found_authors = author_names.collect { |name| Author.find_or_create_by(name: name) }
self.authors = new_or_found_authors
end
Я хочу сохранить порядок списка авторов, которые сохраняются в модели. То есть я хотел бы иметь возможность изменять и переупорядочивать список author_list и извлекать его в этом порядке для представления. Я хочу изменить его ['foo','bar'] или ['bar','foo']. Как я могу это сделать?
В качестве примечания, я попытался использовать act_as_list и добавил столбец позиции в базу данных для авторинга, но безуспешно.