ruby on rails - Activity.order("created_at desc").where(How to include current_user & following_ids?) -


this shows on feed activities users following, want include posts users following + own.

class activitiescontroller < applicationcontroller     def index         @activities = activity.order("created_at desc").where(user_id: current_user.following_ids).paginate(:page => params[:page]) # if take out following_ids list out activities current_user. need both.     end 

activity.rb

class activity < activerecord::base   self.per_page = 20   belongs_to :user   belongs_to :habit   belongs_to :comment   belongs_to :valuation   belongs_to :quantified   belongs_to :trackable, polymorphic: true    def conceal     trackable.conceal   end    def page_number     (index / per_page.to_f).ceil   end  private    def index     activity.order(created_at: :desc).index self   end end 

user.rb

class user < activerecord::base   acts_as_tagger   acts_as_taggable   has_many :notifications   has_many :activities   has_many :activity_likes   has_many :liked_activities, through: :activity_likes, class_name: 'activity',   source: :liked_activity   has_many :liked_comments, through: :comment_likes, class_name: 'comment', source: :liked_comment   has_many :valuation_likes   has_many :habit_likes   has_many :goal_likes   has_many :quantified_likes   has_many :comment_likes   has_many :authentications   has_many :habits, dependent: :destroy   has_many :levels   has_many :combine_tags   has_many :valuations, dependent: :destroy   has_many :comments   has_many :goals, dependent: :destroy   has_many :quantifieds, dependent: :destroy   has_many :results, through: :quantifieds   has_many :notes   accepts_nested_attributes_for :habits, :reject_if => :all_blank, :allow_destroy => true   accepts_nested_attributes_for :notes, :reject_if => :all_blank, :allow_destroy => true   accepts_nested_attributes_for :quantifieds, :reject_if => :all_blank, :allow_destroy => true   accepts_nested_attributes_for :results, :reject_if => :all_blank, :allow_destroy => true   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   attr_accessor :remember_token, :activation_token, :reset_token   before_save   :downcase_email   before_create :create_activation_digest   validates :name,  presence: true, length: { maximum: 50 }   valid_email_regex = /\a[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i   validates :email, presence: true, length: { maximum: 255 },                     format: { with: valid_email_regex },                     uniqueness: { case_sensitive: false }, unless: -> { from_omniauth? }   has_secure_password   validates :password, length: { minimum: 6 }   scope :publish, ->{ where(:conceal => false) }   user.tag_counts_on(:tags)    def count_mastered     @res = habits.reduce(0) |count, habit|     habit.current_level == 6 ? count + 1 : count     end   end    def count_challenged     @challenged_count = habits.count - @res   end      def self.from_omniauth(auth)       where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap |user|         user.provider = auth.provider         user.image = auth.info.image         user.uid = auth.uid         user.name = auth.info.name         user.oauth_token = auth.credentials.token         user.oauth_expires_at = time.at(auth.credentials.expires_at)         user.password = (0...8).map { (65 + rand(26)).chr }.join         user.email = (0...8).map { (65 + rand(26)).chr }.join+"@mailinator.com"         user.save!       end     end    def self.koala(auth)     access_token = auth['token']     facebook = koala::facebook::api.new(access_token)     facebook.get_object("me?fields=name,picture")   end     # returns hash digest of given string.   def user.digest(string)     cost = activemodel::securepassword.min_cost ? bcrypt::engine::min_cost :                                                   bcrypt::engine.cost     bcrypt::password.create(string, cost: cost)   end    # returns random token.   def user.new_token     securerandom.urlsafe_base64   end    # remembers user in database use in persistent sessions.   def remember     self.remember_token = user.new_token     update_attribute(:remember_digest, user.digest(remember_token))   end    # forgets user. not sure if remove   def forget     update_attribute(:remember_digest, nil)   end    # returns true if given token matches digest.   def authenticated?(attribute, token)     digest = send("#{attribute}_digest")     return false if digest.nil?     bcrypt::password.new(digest).is_password?(token)   end    # activates account.   def activate     update_attribute(:activated,    true)     update_attribute(:activated_at, time.zone.now)   end    # sends activation email.   def send_activation_email     usermailer.account_activation(self).deliver_now   end    def create_reset_digest     self.reset_token = user.new_token     update_attribute(:reset_digest,  user.digest(reset_token))     update_attribute(:reset_sent_at, time.zone.now)   end    # sends password reset email.   def send_password_reset_email     usermailer.password_reset(self).deliver_now   end     # returns true if password reset has expired.   def password_reset_expired?     reset_sent_at < 2.hours.ago   end    def good_results_count     results.good_count   end    # follows user.   def follow(other_user)     active_relationships.create(followed_id: other_user.id)   end    # unfollows user.   def unfollow(other_user)     active_relationships.find_by(followed_id: other_user.id).destroy   end    # returns true if current user following other user.   def following?(other_user)     following.include?(other_user)   end  private       def from_omniauth?        provider && uid      end        # converts email lower-case.     def downcase_email        self.email = email.downcase unless from_omniauth?      end      # creates , assigns activation token , digest.     def create_activation_digest       self.activation_token  = user.new_token       self.activation_digest = user.digest(activation_token)     end end 

please let me know if need further explanation or code me =]

you can add current_user following_ids

where(user_id: current_user.following_ids + [current_user.id])


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -