# Merging API data and ActiveRecord data for display in the view

**URL:** <https://www.ruby-forum.com/t/merging-api-data-and-activerecord-data-for-display-in-the-view/175780>\
**Category:** Rails\
**Created:** [September 19, 2009, 2:24pm UTC](https://www.ruby-forum.com/t/merging-api-data-and-activerecord-data-for-display-in-the-view/175780 "2009-09-19T14:24:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [September 19, 2009, 2:24pm UTC](https://www.ruby-forum.com/t/merging-api-data-and-activerecord-data-for-display-in-the-view/175780/1 "2009-09-19T14:24:06Z")

</div>

Hi there,

I’m trying to merge JSON results from an API call in with ActiveRecord  
results for a blended display in a view. I’d like to be able to merge  
the two result sets based on ‘created\_at’ which is is common in both  
the ActiveRecord results and the JSON results from the API. Created\_at  
is the only common element between the two data sets.

Here’s what I have in my controller for retrieving the data:

@diary = current\_user.diary(params[:page])  
start\_date = @diary.first.created\_at.to\_s(:date\_query)  
end\_date = @diary.last.created\_at.to\_s(:date\_query)  
@tweets = JSON.parse(Net::HTTP.get(URI.parse(“http://  
[search.twitter.com/search.json?from=#{current\_user.twitter\_name}](http://search.twitter.com/search.json?from=#%7Bcurrent_user.twitter_name%7D)  
&since=#{start\_date}&until=#{end\_date}”)))  
@tweets = @tweets[‘results’]  
@blend = @diary.entries.concat(@tweets) # tried something like this…  
Not sure this is the best way to go

What’s the best way to merge these two results and sort them by  
'created\_at for display given that  
they are so different? Should I do this in the controller or the  
view? Any guidance or example would be greatly  
appreciated.

Thanks!
