I am looking for a way to edit/add keywords related to an article, inline in Activeadmin.
I have defined a simple many-to-many setup:
class Area < ActiveRecord::Base
has_many :area_keywords
has_many :keywords, :through => :area_keywords
accepts_nested_attributes_for :keywords, :reject_if => :all_blank, :allow_destroy => true
end
class AreaKeyword < ActiveRecord::Base
belongs_to :area
belongs_to :keyword
end
class Keyword < ActiveRecord::Base
has_many :area_keywords
has_many :areas, :through => :area_keywords
end
I would like to add and edit the keywords in en Area form, so I setup this in Aciveadmin:
ActiveAdmin.register Area do
form do |f|
f.inputs "Area details" do
f.input :title
f.input :description
end
f.has_many :keywords do |k|
if k.object.nil?
k.input :word, :label => 'Keyword'
else
k.input :word, :label => k.object.word
k.input :_destroy, :as => :boolean, :label => "delete"
end
end
end
end
This works as expected.
But if I add the same Keyword to two different areas, the Keyword will just be created twice.
When entering a new keyword (in the Area form), I would like it to automatically create a relation to an existing keyword, or create a new keyword, if it does not exist. What would be the best way to go about it?