Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Merging diverse resources for display
1 Post
Merging diverse resources for display

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}&since=#{start_date}&until=#{end_date}")))
@tweets = @tweets['results']
@blend = @diary.entries.concat(@tweets)  

What's the best way to merge these two results given that they are so different? I want to merge them so I can order them by created_at and display them in a view. Any guidance or example would be greatly appreciated.



1 Post
Login to add your message