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?
class Room < ActiveRecord::Base
validates :name, presence: true
has_many :students
accepts_nested_attributes_for :students
end
class Student < ActiveRecord::Base
validates :name, presence: true
belongs_to :room
validates :room, presence: true
end
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]
source
share