Skip to content

Instantly share code, notes, and snippets.

@nwoow
Created July 31, 2015 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nwoow/62f45077f340eb8ffe0b to your computer and use it in GitHub Desktop.
Save nwoow/62f45077f340eb8ffe0b to your computer and use it in GitHub Desktop.
class Message < ActiveRecord::Base
belongs_to :user , :foreign_key => "user_one"
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :feed_likes
has_many :feeds, dependent: :destroy #through: :feed_likes
#has_many :feeds, through: :feed_likes
has_many :market_places
has_many :comments
has_many :messages
has_many :message_replies
# has_many :user_feeds, through: :feed_likes, source: :feed
has_many :active_relationships, class_name: "Relationship",
foreign_key: "follower_id",
dependent: :destroy
has_many :passive_relationships, class_name: "Relationship",
foreign_key: "followed_id",
dependent: :destroy
has_many :following, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
def full_name
if self.name.blank?
self.email
else
self.name
end
end
def feed
following_ids = "SELECT followed_id FROM relationships
WHERE follower_id = :user_id"
Feed.where("user_id IN (#{following_ids})
OR user_id = :user_id", user_id: id)
end
# Follows a user.
def follow(other_user)
active_relationships.create(followed_id: other_user.id)
end
# Unfollows a user.
def unfollow(other_user)
active_relationships.find_by(followed_id: other_user.id).destroy
end
# Returns true if the current user is following the other user.
def following?(other_user)
following.include?(other_user)
end
#logic for private messaging system
acts_as_messageable
def mailboxer_name
self.name
end
def mailboxer_email(object)
self.email
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment