How do I build a JSON object?

后端 未结 2 613
没有蜡笔的小新
没有蜡笔的小新 2021-01-31 10:16

Currently I build a JSON object by doing:

@users = User.all

@users.each do |user|
  @userlist << {
    :id => user.id,
    :fname => user.fname,
            


        
2条回答
  •  心在旅途
    2021-01-31 11:10

      @userlist = @users.map do |u|
        u.attributes.merge!(:contacts=>current_user.contacts)
      end
      json = @userlist.to_json
    

提交回复
热议问题