I have noticed something strange. For I have a rails app which has user model.
User has one lab
Lab belongs to user.
User has two attributes , username and age.
Username is mandatory on creation and age is mandatory when updating.
I am using fields_for and accepts_nested_attributes_for creation of parent as well as child record in a single form.
Lab has four attributes:
:first_reading, :second_reading both mandatory on create , :third_reading, :forth_reading both mandatory on 'update'.
When creating an object, all my validations run on create, username for user and first_reading and second_reading for lab.
When editing the record, when my age is null, validation runs perfect, but when my lab's third and forth reading is null record is submitted. Why is this so? For user's age when blank, validation runs, and for lab's third and forth reading blank, validation doesn't run.
Is there any solution to this.
WHEN CREATING:
It shows the validation errors and the following are logs
Started POST "/users" for ::1 at 2024-01-14 15:01:51 +0500
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"qgh9wx35JaqfAsRULMpBqp4rRlEeqhc8vVWzBkgoq2zV+AjN4WPnqR5l8Z/DUezHcc4UoyjEmAg/FmBoBfpikA==", "user"=>{"username"=>"", "age"=>"", "lab_attributes"=>{"igg_first_reading"=>"", "igm_first_reading"=>"", "igg_second_reading"=>"", "igm_second_reading"=>""}}, "commit"=>"Create User"}
(0.2ms) BEGIN (0.2ms) ROLLBACK
NOW AFTER FILLING ALL REQUIRED FIELDS OF BOTH MODELS, IT CREATES THE RECORD AND FOLLOWING IS THE LOG
Started POST "/users" for ::1 at 2024-01-14 15:06:18 +0500 Processing by UsersController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"brSyTkjpUUkuDoYH0pOCWnnn1mTTgHhcd4V9yQQdPqwiod3WM5IIaU82yFZHPXWlFTgYue9cqhca8Efuzj8HZA==", "user"=>{"username"=>"ans", "age"=>"", "lab_attributes"=>{"igg_first_reading"=>"12", "igm_first_reading"=>"12", "igg_second_reading"=>"", "igm_second_reading"=>""}}, "commit"=>"Create User"}
(0.2ms) BEGIN ↳ app/controllers/users_controller.rb:28 User Create (93.6ms) INSERT INTO "users" ("username", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["username", "ans"], ["created_at", "2024-01-14 10:06:18.084375"], ["updated_at", "2024-01-14 10:06:18.084375"]] ↳ app/controllers/users_controller.rb:28 Lab Create (204.8ms) INSERT INTO "labs" ("igg_first_reading", "igm_first_reading", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["igg_first_reading", 12], ["igm_first_reading", 12], ["user_id", 5], ["created_at", "2024-01-14 10:06:18.179247"], ["updated_at", "2024-01-14 10:06:18.179247"]] ↳ app/controllers/users_controller.rb:28 (16.1ms) COMMIT
WHEN UPDATING
while updating, it shows the user validation of age but not lab readings validation. here are the logs.
Started PATCH "/users/5" for ::1 at 2024-01-14 15:07:29 +0500 Processing by UsersController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"4MKgZPXncHGvI/CilJIu8Ns8Qjl881XLBu0QunWZEKfeAJK1qtbKbBkK/gwGOR+u4sUT0Tws+LDq+UGsMzrLMg==", "user"=>{"username"=>"ans", "age"=>"", "lab_attributes"=>{"igg_first_reading"=>"12", "igm_first_reading"=>"12", "igg_second_reading"=>"", "igm_second_reading"=>"", "id"=>"5"}}, "commit"=>"Update User", "id"=>"5"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 5], ["LIMIT", 1]] ↳ app/controllers/users_controller.rb:64
(0.1ms) BEGIN ↳ app/controllers/users_controller.rb:41 Lab Load (0.1ms) SELECT "labs".* FROM "labs" WHERE "labs"."user_id" = $1 LIMIT $2 [["user_id", 5], ["LIMIT", 1]] ↳ app/controllers/users_controller.rb:41
(0.1ms) ROLLBACK ↳ app/controllers/users_controller.rb:41
AND WHEN I ENTER THE AGE VALUE, IT SUBMITS THE RECORD.
Started PATCH "/users/5" for ::1 at 2024-01-14 15:09:17 +0500 Processing by UsersController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"+8rUqxJrMdoFrbjw5AZTfM8e/zdELcj1SNowy6EpF6mEOqGl7vHz2YTKjTsLnf4RIPutxXJDR8HKmeOl7PveVQ==", "user"=>{"username"=>"ans", "age"=>"8", "lab_attributes"=>{"igg_first_reading"=>"12", "igm_first_reading"=>"12", "igg_second_reading"=>"", "igm_second_reading"=>"", "id"=>"5"}}, "commit"=>"Update User", "id"=>"5"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 5], ["LIMIT", 1]] ↳ app/controllers/users_controller.rb:64 (0.1ms) BEGIN ↳ app/controllers/users_controller.rb:41 Lab Load (0.3ms) SELECT "labs".* FROM "labs" WHERE "labs"."user_id" = $1 LIMIT $2 [["user_id", 5], ["LIMIT", 1]] ↳ app/controllers/users_controller.rb:41
User Update (0.3ms) UPDATE "users" SET "age" = $1, "updated_at" = $2 WHERE "users"."id" = $3 [["age", 8], ["updated_at", "2024-01-14 10:09:17.208441"], ["id", 5]] ↳ app/controllers/users_controller.rb:41 (33.2ms) COMMIT ↳ app/controllers/users_controller.rb:41
NOTE => WHEN UPDATING IF I CHANGE THE EXISTING VALUES OF LAB OR ADD ANY FIELD VALUE (THIRD OR FOURTH READING), THEN VALIDATION RUNS AND IT SHOWS ME ERROR AND IF I DONT TOUCH THE LAB RECORD, VALIDATION DONT RUN
This is my github repo link. You can go through the structure from here. https://github.com/muhammadans414414/Demo


