mardi 24 janvier 2017

Select an object nested attribute in create Active admin

Nested attribute

I have a User that has many book, and a book belongs to a User. I want to create a user from active admin and select a book that was already created and it has no user.

User class :

class User < ActiveRecord::Base
  has_many :books
  accepts_nested_attributes_for :books 
end

Book class :

class Book < ActiveRecord::Base
  belongs_to :user
end

Active Admin User:

ActiveAdmin.register User do
  form do |f|
    f.inputs do
      ...
      f.has_many :books do |referral|
        referral.input :name
      end
      ...
    end
  end
end

I have added a autocomplete that retrieves the names of the books, The problem is if I select one book and this book has no user associated I got an error saying that the book is already taken because it tries to create a new book with the specific name.

Aucun commentaire:

Enregistrer un commentaire