0

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": "abc@gmail.com",
      "mobile": 9876543210,
      "gender":"M"
    },
    {
      "name":"def",
      "age": 26,
      "email": "def@gmail.com",
      "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": "abc@gmail.com",
            "mobile": 9876543210,
            "gender":"M",
            "projects":
                [
                {
                    "projectname":"abc",
                    "type":"abc"
                },
                {
                    "projectname":"def",
                    "type":"abc"
                },
                {
                    "projectname":"ghi",
                    "type":"abc"
                }
        ]
    },
    {
        "name":"def",
        "age": 26,
        "email": "def@gmail.com",
        "mobile": 9876543210,
        "gender":"F",
        "projects":
            [
            {
                "projectname":"abc",
                "type":"abc"
            },
            {
                "projectname":"def",
                "type":"abc"
            },
            {
                "projectname":"ghi",
                "type":"abc"
            }
        ]
    }
    ]
}
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Cyber
  • 4,844
  • 8
  • 41
  • 61
  • You're not sending anything with a `user` key; you're just sending an array of users. In other words, how would Rails find anything under a `user` key? – Dave Newton Jan 19 '13 at 17:36
  • I tried naming my array to user,but it still not saving...Please see my updated JSON Format.. – Cyber Jan 19 '13 at 17:43
  • So, can you create a single `User` passing it an array of individual user data? – Dave Newton Jan 19 '13 at 17:46
  • Still the same problem....data not saving, only one entry with created and updated time. – Cyber Jan 19 '13 at 17:51
  • My question was rhetorical; I'm asking you to think about (a) the data you're actually sending, (b) how it's named, and (c) how you can actually use it. – Dave Newton Jan 19 '13 at 18:00
  • No need to call `to_json` on the hash. – Damien Roche Jan 19 '13 at 18:03
  • In my application datas are commming from iPad (Front end) in json format,there is an option for creating users and so many other stuffs(if i get how to save multiple users ,i can apply that to other stuffs also).If i save single user at a time it will take some network delay, so i thought of Posting Multiple Users so as to reduce network traffic . – Cyber Jan 19 '13 at 18:06
  • @VinTV see my recent answer. I suggest you use https://github.com/zdennis/activerecord-import gem. – Damien Roche Jan 19 '13 at 18:10

2 Answers2

2

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
Community
  • 1
  • 1
Damien Roche
  • 13,189
  • 18
  • 68
  • 96
  • What will happen if User has nested attributes..like (has_many :projects) . So if i tried to save like that it is not working.Any solution. – Cyber Jan 19 '13 at 18:43
  • Please see my Updated Post (Nested Attributes Saving under User). – Cyber Jan 19 '13 at 18:48
0

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
Yunwei.W
  • 1,589
  • 1
  • 14
  • 31