在rails中创建用户操作流

时间:2011-12-19 21:38:34

标签: ruby-on-rails-3

我正在尝试在我的rails App中创建一系列操作。以下是我用来生成用户收到的喜欢列表的帮助方法,以及用户朋友写的故事列表。

稍后,我只是将这些组合成一个数组以在视图中显示。但是,我希望按日期排序我的最终数组,但是当我在前面的方法中使用.map时,我无法弄清楚如何在那里得到一个我可以排序的日期对象。

def get_stream(current_user)
    stories = get_friends_stories(current_user)
    likes = get_likes(current_user)
    stream = [stories, likes]
    stream.flatten
  end

def get_likes(user)
    stories = get_stories(user)
    likes = Like.find_all_by_story_id(stories)
    hash = likes.map {|like| "#{like.user.display_name} liked your story #{like.story.title}" }
  end

  def get_friends_stories(user)
    friends = get_friends(user)
    friend_ids = friends.map {|f| f.friend_id }
    stories = Story.find_all_by_user_id(friend_ids)
    hash = stories.map {|story| "#{story.user.display_name} wrote a story called #{story.title}" } 
  end

1 个答案:

答案 0 :(得分:0)

只是一个简单的改变似乎有效:

def get_stream(current_user)
    stories = get_friends_stories(current_user)
    likes = get_likes(current_user).sort_by {|l| l.created_at }
    streams = stories + likes
    streams.sort_by(&:created_at)
  end