Rails - activeadmin, duplication of has_many records when updating the "parent" record

My models are as follows:

class Project < ActiveRecord::Base
  has_many :project_images
  accepts_nested_attributes_for :project_images
end

class ProjectImage < ActiveRecord::Base
  belongs_to :project
  mount_uploader :image, ImageUploader
end

Here is the activeadmin file:

ActiveAdmin.register Project do
  remove_filter :projects_sectors
  permit_params :title, :info, :case_study, project_images_attributes: [:image, :cover]

  index do
    column :title
    actions
  end

  form :html => { :enctype => "multipart/form-data" } do |f|
    f.inputs "Project" do
    f.input :title
    f.input :info
    f.input :case_study, :as => :file
  end

  f.inputs "Images" do
    f.has_many :project_images, :allow_destroy => true, :heading => false, :new_record => true do |img_f|
      img_f.input :image, :as => :file , :hint => f.template.image_tag(img_f.object.image)
      img_f.input :cover
    end
  end
  f.actions
end


end

The problem is that when I just edit the project and click on the update project, it simply duplicates all the records that exist for the relationship at this point. For instance. if I have 2 images under 1 project, after changing the word, the name of the project, I will have 4 images.

Hope this is a problem. I would really appreciate it if someone could help me.

Thank you very much in advance.

+4
source share
3 answers

You must enable image id: project_images_attributes: [:id, :image, :cover]

, , .

+13

, , CarrierWave. "?". . , .

0
ActiveAdmin.register Project do
  controller do
    def apply_filtering(chain)
      super(chain).distinct
    end
  end

  # your code
end
0
source

Source: https://habr.com/ru/post/1544673/


All Articles