I am developing an API in Rails 3 and I got a user model and a message model. I want the developer to be able to get all info about the user as well as the last message from the user. How can I do this? How can I "include" the messages into the user output?
How can this code be modified to suit my purpose?
def index
@contacts = current_user.contacts
@users = Array.new
@messages = Array.new
@contacts.each do |contact|
user = User.find(contact.friend_id)
@users << user
message = Message.find_by_user_id(contact.friend_id)
@messages << message
end
respond_to do |format|
format.html { render :text => 'Use either JSON or XML' }
format.json { render :json => @users }
format.xml { render :xml => @users }
end
end
Thankful for all input!