Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 7109044cd8
Fetching contributors…

Cannot retrieve contributors at this time

executable file 23 lines (18 sloc) 0.774 kb
class Micropost < ActiveRecord::Base
attr_accessible :content
belongs_to :user
validates :content, presence: true, length: { maximum: 140 }
validates :user_id, presence: true
default_scope order: 'microposts.created_at DESC'
# 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
Jump to Line
Something went wrong with that request. Please try again.