Rails开发:核心应用总结与扩展建议
1. 状态更新流代码实现
在Rails开发中,有一段代码实现了获取用户关注对象的功能,代码如下:
def self.from_users_followed_by(user)
followed_user_ids = "SELECT followed_id FROM relationships
WHERE follower_id = :user_id"
where("user_id IN (#{followed_user_ids}) OR user_id = :user_id",
user_id: user.id)
end
这段代码结合了Rails、Ruby和SQL,能够很好地完成任务。不过需要注意的是,子查询的扩展性并非无限,对于大型网站,可能需要使用后台作业异步生成更新流。
2. 新状态更新流的完成
借助特定代码,状态更新流功能得以完成。以下是主页的代码示例,它创建了一个分页的相关微博更新流,用于视图展示:
# app/controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
if signed_in?
@micropost = current_user.microposts.build
@fee
超级会员免费看
订阅专栏 解锁全文
1002

被折叠的 条评论
为什么被折叠?



