the problem of the implementation of last chapter is:
1. it has a line of code:
following_ids = user.following_ids
this will fetch all the followed users of this user,
but what we need to just if user_id is included in this set.
SQL already optimized things like this, check inclusion in a set of another table.
2. the original way will pull out all the microposts and stick them into an array.
although they are paginated in the view, but in the memory, there is still an array holding all these data.
what we need is it honestly pull out 30 elements at a time.
so we will solve the two problems in this chapter.
1. the solution to both problems involves converting the feed from a class method to a scope
scope is a rails method for restricting database selects based on certain conditions.
for example, to arrange for a method to select all admins, we can add a scope:
scope :admin, where(:admin => true)
then
User.admin
will return all admin users.
2. I know you will ask what's the difference of using scope and class method:
when using independently, they are the same, both pull all admin users from database.
but when using in chains, it will be different:
User.admin.paginate(:page => 1)
this will only pull 30 admins from the database at a time.
so the database did some optimization here, the paginate will happen in the database level.
cool, isn't.
this will make a very bid differences for big site.
3. now it is time to change the from_users_followed_by method using scope:
scope :from_users_followed_by, lambda { |user| followed_by(user) }
note, this scope need to take an argument, so we have to use lambda, a anonymous function.
and the method is a little complicated, so we put it into a method, followed_by(user.)
def self.followed_by(user) following_ids = user.following_ids where("user_id in (#{following_ids}) or user_id = :user_id", {:user => user}) end
but this only make sure the database don't pull all micropost, but it still pull out all followed users.
we should think of a way to replace
following_ids = user.following_ids
we need to use sql text here:
following_ids = %(select followed_id from relationships where follower_id = :user_id)
so the new method is:
def self.followed_by(user) following_ids = %(SELECT followed_id FROM relationships WHERE follower_id = :user_id) where("user_id IN (#{following_ids}) OR user_id = :user_id", { :user_id => user }) end
so the final sql being passed to database is:
SELECT * FROM microposts WHERE user_id IN (SELECT followed_id FROM relationships WHERE follower_id = 1) OR user_id = 1
the database will be more efficient when executing this.
when Micropost.from_users_followed_by(user), it will still return all micropost.
but when chained with paginate or other things, it will be much different.
Micropost.from_users_followed_by(user).paginate(:page => 1)
it will not get all followed users, it just get enough to get the first 30 micropost
4. a way to construct a string: %(.........)
%(fjdkfjdlsjlfdsfjdlsfjlds
fjdsfjdslfdjs)
this is a way to construct string, you can think of it the same as double quotes.
the use case of %() to construct a string is:
a. when you want to show a multiline string.
b. when you want to have "" or "#{jfldjs}" inside a string.
>> puts %(The variable "foo" is equal to "#{foo}".)
all " in this string will be just display, no need to escape,
without %(), it will have to be:
>> "The variable \"foo\" is equal to \"#{foo}\"."
this is not very readable.
发表评论
-
12.3 the status feed
2011-10-30 15:34 8491. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9021. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8681.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7361. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8851. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6941. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 10941. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 724in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 762in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 645again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 6971. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 724whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7351. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 714There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 652start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 640a 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 707in integration test, you can te ... -
8.3 sign up success
2011-10-11 14:39 772Chapter 8.3 this part, we will ...
相关推荐
SQLyog is the most powerful MySQL manager and admin tool, combining the features of MySQL Administrator, phpMyAdmin and other MySQL Front Ends and MySQL GUI tools. 其他版本下载: SQLyog Community ...
12.3.3 几个算法 12.4 Tunis系统 12.5 性能局限性 12.6 习题 第13章 分布式UNIX系统 13.1 卫星处理机系统 13.2 纽卡斯尔连接 13.3 透明型分布式文件系统 13.4 无存根进程的透明分布式模型 13.5 本章小结 ...
The 80x86 MOV Instruction 4.8 - Some Final Comments on the MOV Instructions <br>4.9 Laboratory Exercises 4.9.1 The UCR Standard Library for 80x86 Assembly Language Programmers 4.9.2 ...
SQLyog12.3.3安装包
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
12.3.3 Ductility of Intermetallics 634 12.4 Cellular Materials 639 12.4.1 Structure 639 12.4.2 Modeling of the Mechanical Response 639 12.4.3 Comparison of Predictions and Experimental Results 645 ...
12.3.3 Ductility of Intermetallics 634 12.4 Cellular Materials 639 12.4.1 Structure 639 12.4.2 Modeling of the Mechanical Response 639 12.4.3 Comparison of Predictions and Experimental Results 645 ...
11.4.5 Update the Status of the Large Message from SAP BPM 11.5 SAP BPM Application Programming Interface 11.5.1 Prerequisite to Using the SAP BPM API 11.5.2 Implementation Aspects and Examples ...
SQLyog是一款快速而简洁的图形化管理MYSQL数据库的工具软件。软件由业界著名的 Webyog 公司出品,其最大的功能就是可以在任何地点有效地管理用户自己的数据库。软件界面美观简洁、简单全面、实用方便,无需培训,...
SQLyog-12.3.3-0.x86Community
官方离线安装包,亲测可用
官方离线安装包,亲测可用
SQLyog-12.3.3-0.x64Community
SQLyog-12.3.3-0.x64.exe
资源分类:Python库 所属语言:Python 资源全名:feedcrawler-12.3.3.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
- **4.3 使用字母的例子 (Examples for the use of letters in the IP Code)**: 在某些情况下,除了数字外,还会使用字母来表示额外的防护特性。 **4. 防护等级说明** - **5.1 防护接近危险部件 (Protection ...
资源分类:Python库 所属语言:Python 资源全名:frida-12.3.3-py2.7-win32.egg 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
, the Irish School of Vienna Development Method (VDM). 5.10 The Z Specification Language 5.11 The B-Method 5.12 Predicate Transformers andWeakest Pre-Conditions 5.13 The Process Calculi 5.14 Finite ...
1.8 The Rest of the Book 18 PART I HARDWARE 19 2 Techniques for Designing Energy-Aware MPSoCs 21 Mary Jane Irwin, Luca Benini, N. Vijaykrishnan, and Mahmut Kandemir 2.1 Introduction 21 2.2 Energy-...