开发者

How can I filter through these posts then add them back into a list?

How can I get this query to work where it goes through the checks before adding it to the posts array? I receive the following error "can't convert Post into Array". I'm assuming there may be a better way to query this but I am unsure of how to do so.

This is in in the user model and I am calling this in my home_controller as current_user.personal_feed and then attempting to display each result.

Also, I am not having any problem querying the posts by the users "friends" 开发者_JAVA技巧just having issues only adding the posts that pass certain parameters. Such as they must have a /slashtag in them and the user must also subscribe to that slashtag

def personal_feed
      posts = []
      # cycle through all posts (of the users "friends) & check if the user wants to see them
      Post.find(:all, :conditions => ["user_id in (?)", friends.map(&:id).push(self.id)], :order => "created_at desc").each do |post|
        # first checkpoint: does this post contain a /slashtag
        post.message.scan(%r{(?:^|\s+)/(\w+)}).map {|element|
          # second checkpoint: does this user subscribe to any of these slashtags?
          if self.subscriptions.find_by_friend_id_and_slashtag(post.user.id, element[0])
            posts += post
          end
        }
      end
    end

I have changed the code to this.

  def personal_feed
    posts = []
    # cycle through all posts (of the users "friends) & check if the user wants to see them
    Post.find(:all, :conditions => ["user_id in (?)", friends.map(&:id).push(self.id)], :order => "created_at desc").each do |post|
      # first checkpoint: does this post contain a /slashtag
      post.message.scan(%r{(?:^|\s+)/(\w+)}).map {|element|
        # second checkpoint: does this user subscribe to any of these slashtags?
          posts << post if self.subscriptions.find_by_friend_id_and_slashtag(post.user.id, element[0])
      }
    end

end

It does not raise any errors but it does not run the posts through my conditions. Every post by the users friends is still being displayed even though they do not subscribe to that individual subscription.


def personal_feed
    if user_signed_in?
      @good_friends = []
      current_user.friends.each do |f|
        @good_friends << f #if some condition here
      end 
    else
       #cannot find friends because there is not a current user.
       #might want to add the devise authenticate user before filter on this method
    end 
end

Find the current user, then loop over their friends, only add them to an array if xyz.


  def personal_feed
        posts = []
        # cycle through all posts (of the users "friends) & check if the user wants to see them
        Post.find(:all, :conditions => ["user_id in (?)", friends.map(&:id).push(self.id)], :order => "created_at desc").each do |post|
          # first checkpoint: does this post contain a /slashtag
          post.message.scan(%r{(?:^|\s+)/(\w+)}).map {|element|
            # second checkpoint: does this user subscribe to any of these slashtags?
            posts << post if self.subscriptions.find_by_friend_id_and_slashtag(post.user.id, element[0])
          }
        end
        posts = posts
  end

Final code snippet that worked.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜