0

This is a bit of a design question I suppose:

I've got an application that displays the stories(statuses) of the user's friends. Those friends also post pictures to albums that I'd like to combine into the same index. If the indexed item (status or picture) is a picture I've got a display set up through a .each statement, and then similarly with statuses but with a different display. So I've got to be able to differentiate the two on the page but I need the two combined and then ordered by created_at, similar to facebook's wall feed.

in my controller I've got the variables being passed in

@statuses = Status.order('created_at desc')
@pictures = Picture.order('created_at desc') 

and then in my view I have (dulled-down version)

@pictures.each do |picture|
...do one thing...
end 

@statuses.each do |status|
...do something different...
end

How would I go about rendering both @statuses and @pictures in the same view and ordering them based on the created_at field but while displaying each one with it's own display?

Seph Cordovano
  • 1,824
  • 1
  • 16
  • 19
  • There's a few different ways of doing this - several of them already n StackOverflow. Here's one of them: http://stackoverflow.com/questions/2246479/creating-feeds-from-multiple-different-rails-models I'd recommend using stack-overflows search bar (top right) to search for other examples – Taryn East Mar 09 '15 at 04:02
  • The big difference I saw in that article, which I did find before posting this question here, is that I need to do different things for each view. I can do a join to get them all in the same table and easily run that through a .each conditional but I need to do different things for each table's display while intermixing them based on creation date/time. – Seph Cordovano Mar 09 '15 at 04:10
  • None of that was clear in the question you asked... be specific if you have more information, share it so we don't duplicate your effort. ;) – Taryn East Mar 09 '15 at 04:12
  • If all you want is to use two different partials... then just use two different partials.... Use a conditional eg `render partial => (thing.respond_to?(:photo) ? 'picture' : 'status')`... if you mean something else - then please explain more. – Taryn East Mar 09 '15 at 04:14

2 Answers2

0

The simplest solution would be to use the code below, but the it won't be good with performance.

@result = Status.all + Picture.all
@result.sort_by(&:created_at)

and in the view use and if condition or conditional partial.

render partial => (thing.respond_to?(:photo) ? 'picture' : 'status')
coderhs
  • 4,357
  • 1
  • 16
  • 25
0

After further research, I used this hack-together/solution I found here on SO.

which uses:

@combinedfeed = (Status.all + Picture.all).sort{|a,b| b.created_at <=> a.created_at }

this is for descending order. Switch out .all to limit as needed.

then, I add a column to each database with a default of if it's type, which for me personally will come in handy in a few updates we have planned. Add these to the respective tables:

add_column :pictures, :feedtype, :string, :default => "image"
add_column :statuses, :feedtype, :string, :default => "status"

then, in the view I do something like:

@combinedfeed.each do |value|
  if value.feedtype == "image"
    ...place content for picture loop here, replacing picture with value
  ie ->old:  if signed_in? && (current_user == picture.user)
       new:  if signed_in? && (current_user == value.user) 
  end
  elsif value.feedtype == "status"
    ...place content for picture loop here replacing status with value
  end
end

Hopefully this helps someone else out. I'm sure this isn't the best solution performance-wise but it gets the job done. If anyone has any better solutions to achieve this same result that would handle better on a high volume site, please share!

Community
  • 1
  • 1
Seph Cordovano
  • 1,824
  • 1
  • 16
  • 19