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.
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 816the problem of the implementati ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9101. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8761.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7471. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8931. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6981. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 11031. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 733in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 768in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 657again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 7051. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 732whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7411. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 722There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 655start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 643a session is a semi-permanent c ... -
what test framework should you use?
2011-10-11 16:56 0for integration test, i have no ... -
what test framework should you use?
2011-10-11 16:56 0<p>for integration test, ... -
8.4 rspec integration tests
2011-10-11 16:53 714in integration test, you can te ... -
8.3 sign up success
2011-10-11 14:39 776Chapter 8.3 this part, we will ...
相关推荐
标题中的"Location12.3.zip"表明这是一个与iOS 12.3系统相关的软件包,特别是关于位置服务或者地理位置的。"Location"可能是指该压缩包包含的是用于定位服务的框架、库或者配置文件,这在开发或调试iOS应用时可能会...
iOS 12.3是苹果公司为iPhone、iPad和iPod touch等设备推出的操作系统更新版本。这个系统更新在用户界面、性能优化、安全性和新功能方面都有所改进,旨在提供更稳定、更高效的用户体验。iOS 12.3的发布是苹果对持续...
《ReportBulider12.3在Delphi 2010中的应用及源码解析》 ReportBulider12.3是一款强大的报表生成工具,尤其在Delphi开发环境中,它为开发者提供了丰富的报告设计和自定义功能。Delphi 2010作为一个经典的集成开发...
标题“真机调试包12.3 device support”指的是针对iOS 12.3版本的设备支持文件,这是一组用于Xcode进行真机调试的必要组件。 Xcode是Apple官方提供的集成开发环境(IDE),用于编写iOS、iPadOS、macOS、watchOS以及...
【描述】中提到"实测很容易注册成功,只是你要在网上寻找屏蔽后台联查的软件",这暗示了在安装和激活金蝶KIS ZYB 12.3时可能需要采取一些特殊步骤。通常,商业软件会通过网络验证其合法性,防止未授权的复制和使用。...
《iOS 12.3 更新详解与驱动问题解决》 在科技日新月异的今天,苹果公司的操作系统iOS也不断进行着迭代更新。近期,我们迎来了iOS 12.3的重大更新,旨在提升系统的稳定性和性能,同时也带来了一些新的功能和改进。...
标题中的"Location IOS12.3驱动"指的是iOS 12.3版本中用于处理Location服务的相关驱动程序。这些驱动程序是操作系统与硬件之间的桥梁,确保设备能够准确获取和处理位置数据。 iOS系统每隔一段时间会发布新版本,以...
标题中的"12.3.7z"很可能指的是一个压缩包文件,包含了适用于iOS 12.3版本的驱动程序。"location cleaned12.3驱动"可能意味着这是针对iOS 12.3版本的定位服务相关的驱动更新或者优化。 描述中的"ios12.3驱动文件...
Xcode SDK 12.3 是苹果开发人员用于构建iOS、iPadOS、macOS、watchOS和tvOS应用程序的关键工具。这个版本的SDK是Apple在持续优化和改进其开发环境的一部分,为开发者提供了最新的特性和工具,以提高开发效率和应用...
iOS12.3 真机调试包,适配最新的iOS系统,可以满足12.3开发者使用 将文件放在 /Users/xxx/Library/Developer/Xcode/iOS DeviceSupport位置,放置完成后重新启动xcode即可
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
《iTunes 12.3.2.35:历史版本的应用管理利器》 iTunes,作为苹果公司推出的一款多媒体管理软件,一直以来都是Apple设备用户不可或缺的工具。尤其是在早期,iTunes扮演着至关重要的角色,它不仅负责音乐、电影、...
这个压缩包“TransMac 12.3.rar”包含了TransMac的版本12.3,这是一个更新版本,可能包含了一些性能提升和新功能。 在IT领域,了解如何在非Mac操作系统上处理Mac格式的文件和驱动器是相当重要的。通常,Windows系统...
10. 协议条款,只需要点“勾”I accept the terms of the License Agreement ,才能点击“Next>”开始安装。 三、SolarWinds 配置 Wizard 1. 点击“NEXT”下一步,填写 SQL 账号密码(在安装 SQL 的时候输入的)。...
金蝶KIS专业版12.3装机图解 金蝶KIS专业版12.3装机图解是金蝶软件公司出品的一款专业财务管理软件,旨在帮助企业 quản lý财务、资产、库存、生产、采购等方面的业务。以下是金蝶KIS专业版12.3装机图解的详细安装...
"15042059_12.3 (测试版).zip"这个压缩包文件很显然是一个针对iOS 12.3系统的测试版本调试包。下面将详细解释相关知识点。 1. **测试版调试**:测试版调试是软件开发的一个阶段,主要是为了发现并修复潜在的错误、...
【标题】"T+12.3一百站点 可升级.zip"所指的是一款名为“畅捷通T+12.3”的企业管理软件的更新版本,适用于100个站点的规模。这个压缩包文件包含了该软件的升级资料,允许用户从现有的版本升级到T+12.3版本。"一百...
在iOS 12.3版本中,Location服务得到了更新和优化,确保了更好的性能和用户体验。"location ios 12.3 .zip"这个压缩包文件显然包含了针对这一特定版本的Location驱动程序,它被设计用于改善位置模拟功能。 位置模拟...
《旧版神器 iTunes 12.3.35 for Windows 64位:官方正版的非越狱iOS软件管理》 在iOS设备的管理和应用安装领域,iTunes曾是无可替代的重要工具,尤其是对于那些追求特定历史版本软件的用户而言。本文将详细探讨“旧...
这个“Xcode12.3最新真机/模拟器包”包含了开发iOS应用所需的全套工具,包括源代码编辑器、界面设计工具、编译器、调试器以及iOS模拟器等关键组件。 Xcode 12.3是一个重要的更新,它针对Apple的最新操作系统版本...