`
peryt
  • 浏览: 54347 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • waiting: 既然都指定了dataType为'script'那就不必特别在b ...
    jQuery

12.3 the status feed

 
阅读更多

1. we need to get all the micropost of the users followed by current user.

 

Micropost.from_users_followed_by(user)

 so we can write a test this method.

 

describe Micropost do
  .
  .
  .
  describe "from_users_followed_by" do

    before(:each) do
      @other_user = Factory(:user, :email => Factory.next(:email))
      @third_user = Factory(:user, :email => Factory.next(:email))

      @user_post  = @user.microposts.create!(:content => "foo")
      @other_post = @other_user.microposts.create!(:content => "bar")
      @third_post = @third_user.microposts.create!(:content => "baz")

      @user.follow!(@other_user)
    end

    it "should have a from_users_followed_by class method" do
      Micropost.should respond_to(:from_users_followed_by)
    end

    it "should include the followed user's microposts" do
      Micropost.from_users_followed_by(@user).should include(@other_post)
    end

    it "should include the user's own microposts" do
      Micropost.from_users_followed_by(@user).should include(@user_post)
    end

    it "should not include an unfollowed user's microposts" do
      Micropost.from_users_followed_by(@user).should_not include(@third_post)
    end
  end
end

 

2. since the feed it self lives in the user model, so we still need test for the user model:

 

describe User do
  .
  .
  .
  describe "micropost associations" do
    .
    .
    .
    describe "status feed" do

      it "should have a feed" do
        @user.should respond_to(:feed)
      end

      it "should include the user's microposts" do
        @user.feed.should include(@mp1)
        @user.feed.should include(@mp2)
      end

      it "should not include a different user's microposts" do
        mp3 = Factory(:micropost,
                      :user => Factory(:user, :email => Factory.next(:email)))
        @user.feed.should_not include(mp3)
      end

      it "should include the microposts of followed users" do
        followed = Factory(:user, :email => Factory.next(:email))
        mp3 = Factory(:micropost, :user => followed)
        @user.follow!(followed)
        @user.feed.should include(mp3)
      end
    end
    .
    .
    .
  end
end

 

3. the implemention of feed method is easy, we just defer to from_users_followed_by method.

 

  def feed
    Micropost.from_users_followed_by(self)
  end

 

4. next we will implement this method:

the sql is like this:

 

SELECT * FROM microposts
WHERE user_id IN (<list of ids>) OR user_id = <user id>

before, we have seen:

Microposts.where("user_id = ?", id)

  here, it should be:

 

where("user_id in (#{following_ids}) or user_id = ?", user)

 note

a. we omitte Micropost, since this method is inside the class.

b. we used the rails convention if using user instead of user.id.

 

so we need to map the following user ids into an array.

map method will rescue us here:

 

[1,2,3,4].map{|i| i.to_s}

 since this structure is so common, ruby makes a shortcut for it:

 

[1,2,3,4].map(&:to_s)

so we can use:

User.first.following.map(&:id)

 but rails make it more convenient, 

 

User.first.following_ids

 so, in the end, our method will look like this:

 

def self.from_users_followed_by(user)
  following_ids = user.following_ids
  where("user_id in (#{following_id}) or user_id = ?", user)
end
 

5. now it works!

but we can refactor it:

where method can take a hash as argument.

 

where(:user_id => user.following.push(user))

 this is much more neater.

 

 

it works, but when a user is following 5000 users, it will fetch all the 5000 users, then get all the microposts of the 5000 users, this is not good, we will refactor it in next chapter.

分享到:
评论

相关推荐

    Location12.3.zip

    标题中的"Location12.3.zip"表明这是一个与iOS 12.3系统相关的软件包,特别是关于位置服务或者地理位置的。"Location"可能是指该压缩包包含的是用于定位服务的框架、库或者配置文件,这在开发或调试iOS应用时可能会...

    iOS 12.3真机包

    iOS 12.3是苹果公司为iPhone、iPad和iPod touch等设备推出的操作系统更新版本。这个系统更新在用户界面、性能优化、安全性和新功能方面都有所改进,旨在提供更稳定、更高效的用户体验。iOS 12.3的发布是苹果对持续...

    ReportBulider12.3 form Delphi 2010

    《ReportBulider12.3在Delphi 2010中的应用及源码解析》 ReportBulider12.3是一款强大的报表生成工具,尤其在Delphi开发环境中,它为开发者提供了丰富的报告设计和自定义功能。Delphi 2010作为一个经典的集成开发...

    真机调试包12.3 device support

    标题“真机调试包12.3 device support”指的是针对iOS 12.3版本的设备支持文件,这是一组用于Xcode进行真机调试的必要组件。 Xcode是Apple官方提供的集成开发环境(IDE),用于编写iOS、iPadOS、macOS、watchOS以及...

    ios12.3.zip

    《iOS 12.3 更新详解与驱动问题解决》 在科技日新月异的今天,苹果公司的操作系统iOS也不断进行着迭代更新。近期,我们迎来了iOS 12.3的重大更新,旨在提升系统的稳定性和性能,同时也带来了一些新的功能和改进。...

    Location IOS12.3驱动

    标题中的"Location IOS12.3驱动"指的是iOS 12.3版本中用于处理Location服务的相关驱动程序。这些驱动程序是操作系统与硬件之间的桥梁,确保设备能够准确获取和处理位置数据。 iOS系统每隔一段时间会发布新版本,以...

    12.3.7z ios12.3驱动文件,location cleaned12.3驱动。。

    标题中的"12.3.7z"很可能指的是一个压缩包文件,包含了适用于iOS 12.3版本的驱动程序。"location cleaned12.3驱动"可能意味着这是针对iOS 12.3版本的定位服务相关的驱动更新或者优化。 描述中的"ios12.3驱动文件...

    Xcode SDK 12.3.zip

    Xcode SDK 12.3 是苹果开发人员用于构建iOS、iPadOS、macOS、watchOS和tvOS应用程序的关键工具。这个版本的SDK是Apple在持续优化和改进其开发环境的一部分,为开发者提供了最新的特性和工具,以提高开发效率和应用...

    12.3 (16F5117h).zip

    iOS12.3 真机调试包,适配最新的iOS系统,可以满足12.3开发者使用 将文件放在 /Users/xxx/Library/Developer/Xcode/iOS DeviceSupport位置,放置完成后重新启动xcode即可

    RTL8723BE drivers for opensuse12.3

    The driver of RTL8723BE for Opensese12.3,Before the install of this drivers,you must get root privilege.Then,you can run the shell file:install.sh

    iTunes12.3.2.35.rar

    《iTunes 12.3.2.35:历史版本的应用管理利器》 iTunes,作为苹果公司推出的一款多媒体管理软件,一直以来都是Apple设备用户不可或缺的工具。尤其是在早期,iTunes扮演着至关重要的角色,它不仅负责音乐、电影、...

    TransMac 12.3.rar

    这个压缩包“TransMac 12.3.rar”包含了TransMac的版本12.3,这是一个更新版本,可能包含了一些性能提升和新功能。 在IT领域,了解如何在非Mac操作系统上处理Mac格式的文件和驱动器是相当重要的。通常,Windows系统...

    solarwinds-NPM.12.3安装流程

    10. 协议条款,只需要点“勾”I accept the terms of the License Agreement ,才能点击“Next&gt;”开始安装。 三、SolarWinds 配置 Wizard 1. 点击“NEXT”下一步,填写 SQL 账号密码(在安装 SQL 的时候输入的)。...

    金蝶KIS专业版12.3装机图解

    金蝶KIS专业版12.3装机图解 金蝶KIS专业版12.3装机图解是金蝶软件公司出品的一款专业财务管理软件,旨在帮助企业 quản lý财务、资产、库存、生产、采购等方面的业务。以下是金蝶KIS专业版12.3装机图解的详细安装...

    15042059_12.3 (测试版).zip

    "15042059_12.3 (测试版).zip"这个压缩包文件很显然是一个针对iOS 12.3系统的测试版本调试包。下面将详细解释相关知识点。 1. **测试版调试**:测试版调试是软件开发的一个阶段,主要是为了发现并修复潜在的错误、...

    T+12.3一百站点 可升级.zip

    【标题】"T+12.3一百站点 可升级.zip"所指的是一款名为“畅捷通T+12.3”的企业管理软件的更新版本,适用于100个站点的规模。这个压缩包文件包含了该软件的升级资料,允许用户从现有的版本升级到T+12.3版本。"一百...

    location ios 12.3 .zip

    在iOS 12.3版本中,Location服务得到了更新和优化,确保了更好的性能和用户体验。"location ios 12.3 .zip"这个压缩包文件显然包含了针对这一特定版本的Location驱动程序,它被设计用于改善位置模拟功能。 位置模拟...

    旧版神器 iTunes 12.3.35 win64位

    《旧版神器 iTunes 12.3.35 for Windows 64位:官方正版的非越狱iOS软件管理》 在iOS设备的管理和应用安装领域,iTunes曾是无可替代的重要工具,尤其是对于那些追求特定历史版本软件的用户而言。本文将详细探讨“旧...

    Xcode12.3最新真机/模拟器包

    这个“Xcode12.3最新真机/模拟器包”包含了开发iOS应用所需的全套工具,包括源代码编辑器、界面设计工具、编译器、调试器以及iOS模拟器等关键组件。 Xcode 12.3是一个重要的更新,它针对Apple的最新操作系统版本...

    旧版神器 iTunes 12.3.35 win32位

    《旧版神器iTunes 12.3.35 Win32位:官方正版与旧版软件安装指南》 在IT行业中,对于某些用户来说,旧版本的软件往往具有不可替代的魅力,特别是在iOS设备管理领域,iTunes 12.3.35 win32位便是这样一个备受推崇的...

Global site tag (gtag.js) - Google Analytics