Search code examples
ruby-on-railsrubygemsrails-admin

Rails Admin: Remove associated records option


I am implementing Rails Admin on my app and i would like to remove the option of seeing associated records. What i want to do is keep the option of adding a new record but not selecting a pre-existing one. The screen shot below shows what i am talking about:

enter image description here

I want to keep the product_variants field but with the only option of the button "Add new product variant"

Thanks !


Solution

  • There's no built in way to do that. What i've done in the past is hide all records that are not associated.

    class Product < ApplicationRecord
      rails_admin do
        edit do
          field :product_variants do
            associated_collection_scope do
              inline_edit false
    
              associated_collection_scope do
                product = bindings[:object]
    
                proc { |scope| scope.where(product: product) }
              end
            end
          end
        end
      end
    end
    

    I'm guessing the associations of course, but that should point you in the right direction.