Search code examples
ruby-on-railsruby-on-rails-4acts-as-follower

act_as_followers get all posts of user I'm following


Im trying to get all the Posts from the the Users. I'm following using the act_as_follower gem. The User follows a profile model, and Posts belong to a User.

My User model:

  acts_as_follower

My Profile model the user follows:

  belongs_to :user
  acts_as_followable

Post model:

  belongs_to :user

My Post Controller.rb:

  def follow
    @profile = Profile.find(params[:id])
    current_user.follow(@profile)
    redirect_to :back
  end

  def unfollow
    @profile = Profile.find(params[:id])
    current_user.stop_following(@profile)
    redirect_to :back
  end

Im trying to implement something like this using follows_by_type method provided:

@posts = current_user.follows_by_type('Post').order("created_at DESC")

But the thing is the User follows the Profile model, but here I'm looking for a type 'Post'.

EDIT

In my index controller i'v set up the following:

@favoritePost = Post.where(user_id: current_user.all_follows.pluck(:id))

And in the view iv implemented this:

<% if user_signed_in? %>
   <%@favoritePost.each do |post| %>
      <%= post.title %>
   <% end %>
<% end %>

Solution

  • I worked out this solution, might not be the best, but it works as required.

    In my controller i set this up:

    @following = current_user.all_following
    @followposts = @following.each do |f|
      f.user.id
    end
    

    And in my view I have set this up:

    <% @followposts.each do |f| %>
      <% f.user.posts.each do |g| %>
        <%= g.title %>
      <% end %>
    <% end %>