Permalink
Browse files

Add user following

  • Loading branch information...
1 parent d8c5924 commit faea66cf6b5f3f670931527414954cd643a0e540 @pkrnjevic committed Feb 21, 2012
Showing with 18 additions and 7 deletions.
  1. +2 −1 Gemfile
  2. +2 −2 Gemfile.lock
  3. +1 −0 app/controllers/pages_controller.rb
  4. +13 −4 app/models/micropost.rb
View
@@ -42,7 +42,8 @@ end
gem 'jquery-rails'
group :production do
- gem 'mysql2', '0.3.11'
+# gem 'mysql2', '0.3.11'
+ gem 'pg', '0.12.2'
end
# To use ActiveModel has_secure_password
View
@@ -104,8 +104,8 @@ GEM
treetop (~> 1.4.8)
mime-types (1.17.2)
multi_json (1.0.4)
- mysql2 (0.3.11)
nokogiri (1.5.0)
+ pg (0.12.2)
polyglot (0.3.3)
rack (1.4.1)
rack-cache (1.1)
@@ -196,7 +196,7 @@ DEPENDENCIES
guard-rspec (= 0.5.5)
guard-spork (= 0.3.2)
jquery-rails
- mysql2 (= 0.3.11)
+ pg (= 0.12.2)
rails (= 3.2.1)
rails-footnotes
rspec-rails (~> 2.8.rc)
@@ -1,5 +1,6 @@
class PagesController < ApplicationController
def home
+ @title = "Home"
if signed_in?
@micropost = current_user.microposts.build
@feed_items = current_user.feed.paginate(page: params[:page])
View
@@ -6,8 +6,17 @@ class Micropost < ActiveRecord::Base
validates :user_id, presence: true
default_scope order: 'microposts.created_at DESC'
- def self.from_users_followed_by(user)
- followed_user_ids = user.followed_user_ids.join(', ')
- where("user_id IN (#{followed_user_ids}) OR user_id = ?", user)
- end
+# Returns microposts from the users being followed by the given user.
+ scope :from_users_followed_by, lambda { |user| followed_by(user) }
+
+ private
+
+ # Returns an SQL condition for users followed by the given user.
+ # We include the user's own id as well.
+ def self.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 })
+ end
end

0 comments on commit faea66c

Please sign in to comment.