Skip to content

Commit

Permalink
Finish user following
Browse files Browse the repository at this point in the history
  • Loading branch information
hogemax committed Mar 8, 2018
1 parent d2c786e commit b0243fb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class StaticPagesController < ApplicationController
def home
if signed_in?
@micropost = current_user.microposts.build if signed_in?
@micropost = current_user.microposts.build
#@feed_items = current_user.feed.paginate(:page => params[:page])
@feed_items = current_user.feed.paginate(page: params[:page])
end
Expand Down
11 changes: 11 additions & 0 deletions app/models/micropost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,15 @@ class Micropost < ActiveRecord::Base
default_scope -> { order('created_at DESC') }
validates :content, presence: true, length: { maximum: 140 }
validates :user_id, presence: true

def self.from_users_followed_by(user)
#followed_user_ids = user.followed_user_ids
#where("user_id IN (?) OR user_id = ?", followed_user_ids, user)
#where("user_id IN (:followed_user_ids) OR user_id = :user_id",
# followed_user_ids: followed_user_ids, user_id: 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
end
3 changes: 2 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class User < ActiveRecord::Base
has_many :followers, through: :reverse_relationships, source: :follower

def feed
Micropost.where("user_id = ?", id)
# Micropost.where("user_id = ?", id)
Micropost.from_users_followed_by(self)
end

def following?(other_user)
Expand Down

0 comments on commit b0243fb

Please sign in to comment.