Cyber
Cyber

Reputation: 5020

How to Post array of data all at one time?

I have a Rails 3 Application that is trying to post an array of users, all at one time. I am trying to post through the Postman REST client. If I tried to post a single user at a time it works well, but it is not possible to post multiple users at a time.

This is my User model:

class User < ActiveRecord::Base
  attr_accessible :name,age,email,mobile,gender
end

And my User controller:

respond_to :html , :json

def create
  @user = User.new(params[:user])
  if @user.save
    render :json => { :status => :ok, :message => "User Created Successfully"}.to_json
  end
end

User posting data in JSON format for multiple users:

{
  user:[
    {
      "name":"abc",
      "age": 23,
      "email": "[email protected]",
      "mobile": 9876543210,
      "gender":"M"
    },
    {
      "name":"def",
      "age": 26,
      "email": "[email protected]",
      "mobile": 9876543210,
      "gender":"F"
    }
  ]
}

Is it possible to do this in Rails?

I tried:

def create
  @userlist = User.new(params[:user])
  @userlist.each do |u|
    u.save
  end
  render :json => { :status => :ok, :message => "User Created Successfully"}.to_json
end

but the data is not saved.

Is there any solution?

Nested attributes saving under User:

{
    "users" :[
    {
        "name":"abc",
            "age": 23,
            "email": "[email protected]",
            "mobile": 9876543210,
            "gender":"M",
            "projects":
                [
                {
                    "projectname":"abc",
                    "type":"abc"
                },
                {
                    "projectname":"def",
                    "type":"abc"
                },
                {
                    "projectname":"ghi",
                    "type":"abc"
                }
        ]
    },
    {
        "name":"def",
        "age": 26,
        "email": "[email protected]",
        "mobile": 9876543210,
        "gender":"F",
        "projects":
            [
            {
                "projectname":"abc",
                "type":"abc"
            },
            {
                "projectname":"def",
                "type":"abc"
            },
            {
                "projectname":"ghi",
                "type":"abc"
            }
        ]
    }
    ]
}

Upvotes: 0

Views: 880

Answers (2)

user419017
user419017

Reputation:

As seen here, I'd suggest you bulk insert (depending on the likely amount of users that will be passed at a time), using this gem:

def create
    users = []
    @userlist = params[:users]

    @userlist.each do |u|
       user = User.new(u)
       users << user
    end

    User.import(users)

    render :json => { :status => :ok, :message => "User(s) Created Successfully"}
end

Upvotes: 2

Yunwei.W
Yunwei.W

Reputation: 1599

Ok, i see your edit of the posted params. so do it in your controller like this:

def create
    @userlist = params[:users]
    @userlist.each do |u|
       user = User.new(u)
       user.save!
    end
    render :json => { :status => :ok, :message => "User Created Successfully"}.to_json
end

Upvotes: 0

Related Questions