ActiveAdmin has_many form is not saved if the parent model is new and is not NULL in the child model

I have two models: Roomand Student. Room has_many Students. Student belongs_to Room.

I got an error. The room cannot be empty when I try to add a student to the room while creating a new room.

I assume that after sending the child object (student) is saved until the parent object (room) is saved. Is there a way around an order without removing the NOT NULL setting in room_id? Or is my guess wrong? Or worse, am I doing it wrong?

# app/models/room.rb
class Room < ActiveRecord::Base
  validates :name, presence: true
  has_many :students

  accepts_nested_attributes_for :students
end



# app/models/student.rb
class Student < ActiveRecord::Base
  validates :name, presence: true

  belongs_to :room
  validates :room, presence: true # room_id is set to NOT NULL in database too.

end



# app/admin/room.rb
  form do |f|
    f.semantic_errors *f.object.errors.keys
    f.inputs "Room Details" do
      f.input :name

      f.has_many :students do |student|
        student.input :name
      end
    end

    f.actions
  end

  permit_params :name, students_attributes: [:name]
+4
source share
1 answer

, belongs_to has_many . has_many belongs_to, "" , :)

, :

class Room < ActiveRecord::Base
  has_many :students, :inverse_of => :room
  accepts_nested_attributes_for :students
end

class Student < ActiveRecord::Base
  belongs_to :room, :inverse_of => :students
  validates_presence_of :room
end 
+3

Source: https://habr.com/ru/post/1584520/


All Articles