Adding and listing has_many via associations in Active Admin

I have the following setup:

class User < ActiveRecord::Base

  has_many :device_ownerships, :dependent => :destroy
  has_many :devices, :through => :device_ownerships

end

class device < ActiveRecord::Base
  has_one :device_ownership, :dependent => :destroy
  has_one :user, :through => :device_ownership

end


class deviceOwnership < ActiveRecord::Base
  belongs_to :user  
  belongs_to :device
  validates_uniqueness_of :device_id, :scope => :user_id
  validates_uniqueness_of :user_id, :scope => :device_id
end

      

I am trying to accomplish the following in Active Admin:

In the edit window

1) List of all devices owned by the user, with the ability to delete the device or destroy deviceOwnership

, which connects the device to the user

2) it is possible to create a new user interface device from existing devices (by creating a new one deviceOwnership

).

3) it is possible to create a new device and add it to the user through a new one deviceOwnership

.

I have listed my problems with what I currently have in the comments below:

ActiveAdmin.register User do

  permit_params :email, :password, :password_confirmation, :role,
    device_ownerships_attributes: [:device_id, :user_id],
    devices_attributes: [:device_identifier]

  index do |user|
    user.column :email
    user.column :current_sign_in_at
    user.column :last_sign_in_at
    user.column :sign_in_count
    user.column :role
    actions
  end

  filter :email

  form do |f|
    f.inputs "User Details" do
      f.input :email
      f.input :password
      f.input :password_confirmation
      f.input :role, as: :radio, collection: {Editor: "editor", Moderator: "moderator", Administrator: "administrator"}

  #This one  allows to create new devices but also lists all existing devices with option to modify their device_identifier column which I don't want
      f.has_many :devices, :allow_destroy => true, :heading => 'Themes', :new_record => true do |cf|
        cf.input :device_identifier
      end

  #This one  lists all the devices but no option to remove any of them.
      f.input :devices

  #This one shows dropdownw with existing devices but allows to swap them

      f.has_many :devices, :allow_destroy => true do |device_f|
        device_f.input :device_identifier, :as => :select, :collection => device.all.map{ |device| [device.device_identifier] }, include_blank: false,
      end


      f.actions
    end

  end
end

      

+3


source to share


1 answer


Line

f.has_many :devices, :allow_destroy => true, :heading => 'Themes', :new_record => true do |cf|
  cf.input :device_identifier
end

      

looks like it might do the job. You can check if cf.object is a new entry, and only then can the user change device_identifier

in that case.



cf.input :device_identifier if cf.object.new_record?

or something like cf.input :device_identifier, input_html: { readonly: !cf.object.new_record? }

What do you think?

0


source







All Articles