lundi 28 décembre 2015

Activeadmin, duplicating has_many records

When I use ActiveAdmin to edit one Agency, I can select a City and associates it to the Agency. The city is linked to the Agency, but the city is all the times duplicated in the database.

My models:

# agency.rb
class Agency < ActiveRecord::Base
  has_many :agency_cities
  has_many :cities, through: :agency_cities
  accepts_nested_attributes_for :cities, allow_destroy: true
end

# city.rb
class City < ActiveRecord::Base
  has_many :agency_cities
  has_many :agencies, through: :agency_cities
end

# AgencyCity.rb
class AgencyCity < ActiveRecord::Base
  belongs_to :agency
  belongs_to :city
end

I read the doc of Activeadmin and added the [:id] permit_parameter, but I still have the problem, I'm very confused.

# admin/agency.rb
ActiveAdmin.register Agency do
  permit_params :name, :picture,
    cities_attributes: [:id, :name, :description, :_destroy]

  form do |f|
     f.inputs "Agencies" do
       f.input :picture, as: :file
       f.input :creation_date, label: 'Creation Date'
       f.input :name, label: 'Name'
     end
   end

   f.inputs do
     f.has_many :cities do |k|
       k.input :name, label: 'City',
         as: :select,
         collection: City.all.map{ |u| "#{u.name}"}
       k.input :_destroy, as: :boolean
     end
   end
   f.actions
end

Aucun commentaire:

Enregistrer un commentaire