Ruby on Rails Wednesday, March 2, 2016

On 2 March 2016 at 18:47, 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])

I don't think you want paginate here, and since you are going to sort
it again later there is no point sorting here either.

> @following_activities << current_user.posts.order('created_at
> desc').all

You want + here not << as you just want to concatenate the arrays,
then you will not need to flatten in the next line. Also no need to
sort in line above

> @following_activities = @following_activities.flatten.sort_by
> {|post| post.created_at }
> end

As I said above, no need for the flatten

Also, assuming that you have User has_many following_users and User
has_many posts then you should be able to say something like
User has_many following_posts through following_users ...
You will need a bit more on the end get it to work, not exactly sure
what you need there. Perhaps someone else will know exactly what you
need. Then you will be able to say
current_user.following_posts
to get all those posts.

So you will be able to say
@following_activities = (current_user.posts +
current_user.following_posts).sort....

Colin

>
> 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/CAL%3D0gLt0JXpspH7JKbPN57yYHX8c%3DKS0rmEKjVt1Rh9Ou9%3Do%3DQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment