find(:all) and then add data from another table to the object

Posted by Koning Baard XIV on Stack Overflow See other posts from Stack Overflow or by Koning Baard XIV
Published on 2010-02-19T16:53:02Z Indexed on 2010/04/15 8:03 UTC
Read the original article Hit count: 390

Filed under:
|
|
|

I have two tables:

create_table "friendships", :force => true do |t|
    t.integer  "user1_id"
    t.integer  "user2_id"
    t.boolean  "hasaccepted"
    t.datetime "created_at"
    t.datetime "updated_at"
end

and

create_table "users", :force => true do |t|
    t.string   "email"
    t.string   "password"
    t.string   "phone"
    t.boolean  "gender"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "firstname"
    t.string   "lastname"
    t.date     "birthday"
end

I need to show the user a list of Friendrequests, so I use this method in my controller:

def getfriendrequests
    respond_to do |format|
        case params[:id]
            when "to_me"
                @friendrequests = Friendship.find(:all, :conditions => { :user2_id => session[:user], :hasaccepted => false })
            when "from_me"
                @friendrequests = Friendship.find(:all, :conditions => { :user1_id => session[:user], :hasaccepted => false })
        end
        format.xml  { render :xml  => @friendrequests }
        format.json { render :json => @friendrequests }
    end
end

I do nearly everything using AJAX, so to fetch the First and Last name of the user with UID user2_id (the to_me param comes later, don't worry right now), I need a for loop which make multiple AJAX calls. This sucks and costs much bandwidth. So I'd rather like that getfriendrequests also returns the First and Last name of the corresponding users, so, e.g. the JSON response would not be:

[
  {
    "friendship": {
      "created_at": "2010-02-19T13:51:31Z",
      "user1_id": 2,
      "updated_at": "2010-02-19T13:51:31Z",
      "hasaccepted": false,
      "id": 11,
      "user2_id": 3
    }
  },
  {
    "friendship": {
      "created_at": "2010-02-19T16:31:23Z",
      "user1_id": 2,
      "updated_at": "2010-02-19T16:31:23Z",
      "hasaccepted": false,
      "id": 12,
      "user2_id": 4
    }
  }
]  

but rather:

[
  {
    "friendship": {
      "created_at": "2010-02-19T13:51:31Z",
      "user1_id": 2,
      "updated_at": "2010-02-19T13:51:31Z",
      "hasaccepted": false,
      "id": 11,
      "user2_id": 3,
      "firstname": "Jon",
      "lastname": "Skeet"
    }
  },
  {
    "friendship": {
      "created_at": "2010-02-19T16:31:23Z",
      "user1_id": 2,
      "updated_at": "2010-02-19T16:31:23Z",
      "hasaccepted": false,
      "id": 12,
      "user2_id": 4,
      "firstname": "Mark",
      "lastname": "Gravell"
    }
  }
]   

I thought of a for loop in the getfriendrequests method, but I don't know how to implement this, and maybe there is an easier way. It must also work for XML. Can anyone help me? Thanks

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about response