I have the following model structure:
Composition
has many Score
(Score
belongs to Composition
)Composition
has and belongs to many Countries
(and viceversa)score.rb:
class Score < ApplicationRecord
belongs_to :composition
end
composition.rb:
class Composition < ApplicationRecord
has_many :scores
has_and_belongs_to_many :countries, join_table: :rights_countries
end
country.rb:
class Country < ApplicationRecord
has_and_belongs_to_many :compositions, join_table: :rights_countries
end
In activeadmin, I want to be able to edit the countries of a composition, but in the edit form of its scores.
Of course, the form will import this data from composition, and default inputs will be equal for all the scores (children) of a composition.
I found no way to implement this in activeadmin up to now.
Is this even possible? If yes, is the solution easy or cumbersome?
Following this link, I added an inputs
within an inputs
and updated the corresponding params. I also added accepts_nested_attributes_for :composition
in the score model.
app/models/score.rb
...
accepts_nested_attributes_for :composition
...
app/admin/score.rb
...
permit_params ...,
composition_attributes: [:id, country_ids: []]
...
form do |f|
f.inputs do
...
f.inputs "", for: [:composition, score.composition] do |c|
c.input :countries, as: :select, collection: Country.order_by_name.uniq.map { |p| [p.name, p.id] }
end
end
end
Let me know if there's a cleaner solution.