Currently I build a JSON object by doing:
@users = User.all
@users.each do |user|
@userlist << {
:id => user.id,
:fname => user.fname,
json = User.all( :include => :contacts).to_json( :include => :contacts )
Sorry, let me give a more complete answer for what you're doing...
@users = User.all( :include => :contacts )
@userlist = @users.map do |u|
{ :id => u.id, :fname => u.fname, :lname => u.lname, :photo => u.profile_pic.url(:small), :contacts => u.contacts }
end
json = @userlist.to_json
Ok, so just forget me - I was having a bad day and totally missed the point of your question. You want some JSON that includes two unrelated sets of data. All the users, and the contacts just for the current user.
You want to create a new hash for that then, something like this...
@users = User.all
@userlist = @users.map do |u|
{ :id => u.id, :fname => u.fname, :lname => u.lname, :photo => u.profile_pic.url(:small) }
end
json = { :users => @userlist, :contacts => current_user.contacts }.to_json
@userlist = @users.map do |u|
u.attributes.merge!(:contacts=>current_user.contacts)
end
json = @userlist.to_json