0

I have this PostApplication Model and PostApplication has_many Documents in this below activeadmin resource which contains has_many form.

I want to show various records of Document model based on the boolean is_scanned and I want to create documents in the different tabs through postapplication model only but in the second tab whenever i upload a new file it updates the last existing document

Please Help

ActiveAdmin.register PostApplication do
form do |f|
  tab "Documents" do
    tabs do
      tab "Not Scanned" do
        f.has_many :documents, for: [:documents, f.object.documents.where(is_scanned: false) || Document.new(is_scanned: false)] do |la|
        la.semantic_errors *la.object.errors.keys
        la.input :verified
        la.input :document, as: :file, required: false, :hint => (la.object.new_record? || !la.object.document.exists? ? "" : link_to("Download", "#"))
        la.input :_destroy, as: :boolean, required: false, label: t('remove')
      end
    end

    tab "Scanned" do
      f.has_many :documents, for: [:documents, f.object.documents.where(is_scanned: true) || Document.new(is_scanned: true)] do |la|
        la.semantic_errors *la.object.errors.keys
        la.input :verified
        la.input :document, as: :file, required: false, :hint => (la.object.new_record? || !la.object.document.exists? ? "" : link_to("Download","#"))
        la.input :_destroy, as: :boolean, required: false, label: t('remove')
      end
    end
   end
  end
end
end
Rishabh
  • 75
  • 1
  • 9
  • You can create different associations for scanned and unscanned documents. Say, `has_many :documents_scanned` and `has_many :documents_unscanned`. You will need to pass the required options here though. – Jagdeep Singh Apr 10 '17 at 10:46
  • Because both scanned and unscanned docs are consisting of same columns and they are separated by just a boolean which is **is_scanned** So without creating 2 models what's the possible way to use it – Rishabh Apr 11 '17 at 06:15
  • You won't need to create different models for these associations. You can just pass the condition `is_scanned: true` and `is_scanned: false` when defining the associations using `has_many`. See this: http://stackoverflow.com/questions/2462203/rails-has-many-with-dynamic-conditions – Jagdeep Singh Apr 11 '17 at 07:54

0 Answers0