Search code examples
ruby-on-railsrubyruby-on-rails-4

how to show 'recently visited'?


I have created a web app that has user profiles, where users can search for fellow users based on interests, as well as post + attend events etc.. How might I add a feature where users can see who 'Recently visited' a certain event?

event.rb

class Event < ActiveRecord::Base
  attr_accessible :title, :description, :location, :date, :time, :event_date

  acts_as_commentable
  has_many :comments, as: :commentable
  belongs_to :user
  has_many :event_participants, dependent: :destroy
  has_many :participants, through: :event_participants, source: :user

end

user.rb

class User < ActiveRecord::Base
  include PgSearch

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable,
         :omniauth_providers => [:facebook, :twitter, :linkedin]

  attr_accessible :email, :password, :password_confirmation, :zip, :gender, :remember_me, :first_name, :last_name,
                  :birthday, :current_password, :occupation, :address, :interests, :aboutme, :profile_image,
                  :photos_attributes, :age, :education, :ethnicity

  has_many :authorizations, :dependent => :destroy
  has_many :comments
  has_many :events
  has_many :photos, as: :attachable
  has_many :questions
  has_many :sent_messages, class_name: 'Message', foreign_key: :sender_id
  has_many :received_messages, class_name: 'Message', foreign_key: :receiver_id
  accepts_nested_attributes_for :photos
  mount_uploader :profile_image, ProfileImageUploader

end

event_participants.rb

class EventParticipant < ActiveRecord::Base
  attr_accessible :user_id, :event_id

  belongs_to :user
  belongs_to :event

  validates :user_id, uniqueness: {scope: :event_id}

end

events_controller snippit

class EventsController < ApplicationController
  before_filter :authenticate_user!

  def index
    @users = User.all
    @user = current_user
    @events = Event.all
    @interesting_people = @users.order("random()").first(5)

  end

  def new
    @event = Event.new
  end

  def create
    @event = current_user.events.new(event_params)

    respond_to do |format|
      if @event.save
        format.html { redirect_to :back, notice: 'Event was successfully created.' }
        format.json { render action: 'show', status: :created, location: @event }
        format.js
      else
        format.html { render action: 'new' }
        format.json { render json: @event.errors, status: :unprocessable_entity }
        format.js
      end
    end
  end

  def show
    @users = User.all
    @user = current_user
    @event = Event.find(params[:id])
    @commentable = @event
    @comment = @event.comments.new
    @interesting_people = @users.order("random()").first(5)
  end

Solution

  • Based on your code, it looks like you could do something like:

    @event.event_participants.include(:users).order('created_at desc').limit(X).map(&:user)
    

    You could also try without the include:

    @event.event_participants.order('created_at desc').limit(X).map(&:user)
    

    But that will do N+1 queries. The join will perform better.

    Where in the above code, X would be how many participants you want to show.