Ask A Question

Notifications

You’re not receiving notifications from this thread.

fields_for and accepts_nested_attributes_for in rails

Muhammad Ans asked in Rails

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.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.