我正在使用Ruby on Rails 3.2编写的社交网络。 我创建了一个表PostView,以便按用户计算发布视图。它具有以下列:post_id,user_id,created_at。这个想法是每次用户看到帖子时在该表中存储一个新条目(理想情况下添加他在过去6小时内没有看到它的条件)。
我的问题是我想在用户加载新闻Feed时这样做。由于许多帖子同时被加载,我想做一个请求,为每个帖子创建一个条目(我绝对不想在循环中做一个请求)
有什么想法吗?
答案 0 :(得分:2)
如果我理解你的问题,那么你需要的是将你的代码放在控制器中,如
def show
@post = Post.all
#next you need to do a view count so create a new method
@post_view = PostView.create_new_post_view(@post, current_user)
end
在Post视图模型中
def self.create_new_post_view(post, user)
post.each do |p|
# put your 6 hour logic here as you have to loop through it because your logic must be valid against each post. so against each post you have to validate your logic.
end
end