Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
sample_app_ch9/app/models/micropost.rb
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
16 lines (14 sloc)
563 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Micropost < ActiveRecord::Base | |
before_save { } | |
belongs_to :user | |
default_scope -> { order('created_at DESC') } | |
validates :content, presence: true, length: { maximum: 140 } | |
validates :user_id, presence: true | |
# Returns microposts from the users being followed by the given user. | |
def self.from_users_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.id) | |
end | |
end |