active admin has_one duplication inside the select form

231 Views Asked by At

The input of my active admin code gives me selection of all the addresses duplicated I want the addresses without any duplication inside the select form enter image description here

in case new

  <%= form_for @case,:url => {:action => :create, :id => @user.id} do |f| %>
    Address:<br>
    <%= f.select :address, options_for_select(@user.addresses.all.pluck(:name,:id), @case.address&.id) %><br><br><br>
      Patient First Name:<%= f.text_field :pt_first_name, class: "textbox" ,required: "required" ,placeholder: "Patient Name"%>
      Patient Last Name:<%= f.text_field :pt_last_name, class: "textbox" ,required: "required" ,placeholder: "Patient Name"%>
<% end %>

in active admin case.rb

ActiveAdmin.register Case do
permit_params  :user_id, :product_ids,:step_ids  , :pt_first_name,:pt_last_name, 
:date_received, :due_date, :shade, :mould, :upper_lower,:invoice,:implant_brand, :implant_quantity,address_attributes: [ :name]

    form do |f|
            f.inputs do
                f.input :user
                f.inputs "address", for: [:address, f.object.address || Address.new] do |meta_form|
                meta_form.input :name, :as => :select, :collection => Address.order(name: :desc).collect {|address| [address.name, address.id] }
            end
             
            f.input :pt_first_name
            f.input :pt_last_name
1

There are 1 best solutions below

4
Khaled Hassan On

You need to use distinct .. have a look here for clear explanation

So, this line:

<%= f.select :address, options_for_select(@user.addresses.all.pluck(:name,:id), @case.address&.id) %><br><br><br>

Should be like this:

<%= f.select :address, options_for_select(@user.addresses.distinct.pluck(:name,:id), @case.address&.id) %><br><br><br>

Also note that @user.addresses.all is the same as @user.addresses