Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User < ActiveRecord::Base
- attr_accessible :name, :email, :password, :password_confirmation
- has_secure_password
- has_many :microposts, dependent: :destroy
- has_many :relationships, foreign_key: "followed_id", dependent: :destroy
- has_many :followed_users, through: :relationships, source: "followed_id"
- has_many :reverse_relationships, foreign_key: "followed id",
- class_name: "Relationships",
- dependent: :destroy
- has_many :followers, through: :reverse_relationships, source: :follower
- before_save { |user| user.email = email.downcase }
- before_save :create_remember_token
- validates :name, presence: true, length: { maximum: 50 }
- VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
- validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
- uniqueness: { case_sensative: false }
- validates :password, length: { minimum: 6 }
- validates :password_confirmation, presence: true
- def feed
- Micropost.where("user_id = ?", id)
- end
- def following?(other_user)
- relationships.find_by_followed_id(other_user.id)
- end
- def follow!(other_user)
- relationships.create!(followed_id: other_user.id)
- end
- def unfollow!(other_user)
- relationship.find_by_followed_id(other_user.id).destroy
- end
- private
- def create_remember_token
- self.remember_token = SecureRandom.urlsafe_base64
- end
- end
Add Comment
Please, Sign In to add comment