Ruby on Rails Wednesday, March 2, 2016

How about just adding the current_user.id to the following_ids array? You could then pull all the posts in one shot, and remove the next two lines.

~Marc


~Mahcsig


On Wed, Mar 2, 2016 at 10:47 AM, David Williams <lists@ruby-forum.com> wrote:
I've been trying to combine my own post with the users that I follow in
chronological order DESC. Can you help me build the proper query method
for it?


What I currently have is the code below

def feed
  following_ids = current_user.following_users.map(&:id)
  @following_activities =
Post.where(user_id:following_ids).order("created_at
desc").paginate(page:params[:page])
    @following_activities << current_user.posts.order('created_at
desc').all
    @following_activities = @following_activities.flatten.sort_by
{|post| post.created_at }
end

Feed.html.erb
  <% if @following_activities.any? %>
      <% @following_activities.each do |user| %>
          <%= link_to(image_tag(user.avatar.url(:thumb), style:
'text-decoration: none;', class: 'round-image-50-trendy-warrior'),
user_path(user)) %>
          <%= user.post.username %>
          <%= user.post.body %>
          <%= image_tag(user.post.photo.url(:medium), style: '') %>
      <% end %>
  <% else %>
      <h1>No new posts</h1>
  <% end %>

The approach that I'm using is not causing any server errors. But, I'm
not seeing the post that I've already created, they aren't being loaded.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5317cf6d26c8523f7496dc2931f4d1a7%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CANBna%3DANe41MpEMcJfctpJ8%2Bk4q0sm34YKk8MpHVYAhh4RK7QQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment