12/08/2018, 13:35

Nested Model Validation Problem and Solution

Nested Attribute is one of the helpful feature for Ruby on Rails developers. It gives the privilege to save attributes of a record through its associated parent. You can find details about nested model and nested attribute here. Model Validation Validation of models is an important part ...

ror31.jpg

Nested Attribute is one of the helpful feature for Ruby on Rails developers. It gives the privilege to save attributes of a record through its associated parent. You can find details about nested model and nested attribute here.

Model Validation

Validation of models is an important part of development as it ensures that only valid data are saved into model. For instace, it may be necessary for any application to ensure that every user provide valid email or mailing address. It is a good practice because it prohibits error and use of unpermitted data. But validation in nested model cause a problem. I am explaining it with an example.

Suppose we have two model User and Project where a user can have many project. So relationship between these two model is one to many. So our model description are

app/models/user.rb

class User < ActiveRecord::Base
     has_many :projects
 end

app/models/project.rb

 class Project < ActiveRecord::Base
     belongs_to :user
 end

As there is a has_many relation in user model with project, we can enable nested attribute updating by

app/models/user.rb

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

Now as a good and sincere developer, we will add validation to our models

app/models/user.rb

class User < ActiveRecord::Base
     has_many :projects

     validates_presence_of :email
     validates_presence_of :name
     accepts_nested_attributes_for :projects
 end

app/models/project.rb

 class Project < ActiveRecord::Base
     belongs_to :user

     validates_presence_of :user
     validates_presence_of :name
 end

Ok now model programming is done. We will now move to controller. We will create an user with some projects at the same time. So our user controller will be like

app/controllers/users_controller.rb

 class UsersController < ApplicationController

     def create
        user = User.new(user_params)
        if user.save
          render json: { success: "Created Successfully"),
        else
          render json: user.errors
        end
      end

      def user_params
        params.require(:user).permit :email, :name, projects_attributes: [:name]
      end
 end

Our controller and models are ready and so we are ready to roll. Now we are going to create an user with this JSON request:

curl -X POST -H "Content-Type: application/json" http://localhost:3000/users -d '{"user":{"name": "Ananta Jalil", "email": "yopmail@jalil.com", "projects_attributes": [{"name": "acting"}, {"name": "Business"}]}}

But BANG!! It throws an error, right?? and the error is

{projects.user_id: [projects.attribute.user_id.blank]}             </div>
            
            <div class=

0