0
保存的屬性列表的順序我有兩個模型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
的getter和setter方法我要保持這種得到保存到模型作者列表的順序。也就是說,我希望能夠更改和重新排列author_list,並按照該視圖的順序檢索它。我想要改變它['foo','bar']或['bar','foo']。我怎樣才能做到這一點?
作爲一個說明,我已經嘗試使用acts_as_list並將一個位置列添加到數據庫進行創作,但沒有成功。