Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6745815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:14:57+00:00 2026-05-26T12:14:57+00:00

This is a bit complicated and I’m not sure how to implement it. I

  • 0

This is a bit complicated and I’m not sure how to implement it. I have a User model and a Relationship model. Users are able to “follow” each other (just like twitter). The relationship model is all setup properly and works great.

Next, I have an Event model. Each user has_and_belongs_to_many events (many to many association between users and events). Users “attend” events.

What I would like to do is pull a list of all events that are

  1. being attended by the current_user
  2. are being attended by users that current_user is following.

If possible, I would like to have this list accessible via the User model so I can say current_user.event_feed and it will list all events as mentioned above.

Here are my models:

class Event < ActiveRecord::Base
  attr_accessible :name, 
                  :description, 
                  :event_date, 
                  :location, 
                  :owner_id,
                  :category,
                  :photo

  CATEGORIES = ['Music', 'Outdoors', 'Party']

  has_and_belongs_to_many :users

and relationship model:

class Relationship < ActiveRecord::Base
  attr_accessible :followed_id

  belongs_to :follower, :class_name => "User"
  belongs_to :followed, :class_name => "User"

  validates :follower_id, :presence => true
  validates :followed_id, :presence => true
end

and user model:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me
  attr_accessor :password
  attr_accessible :name, :email, :password, :password_confirmation, :time_zone

  has_and_belongs_to_many :events

  has_many :relationships, :dependent => :destroy,
                             :foreign_key => "follower_id"
  has_many :reverse_relationships, :dependent => :destroy,
                                   :foreign_key => "followed_id",
                                   :class_name => "Relationship"
  has_many :following, :through => :relationships, 
                       :source  => :followed
  has_many :followers, :through => :reverse_relationships,
                       :source  => :follower

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:14:58+00:00Added an answer on May 26, 2026 at 12:14 pm

    This is rails 3 only, but quite elegant (untested, hopefully my memory of habtm relationships is ok).

    class User < ActiveRecord::Base
      # ...
    
      def event_feed
        ids = self.followers.collect(&:id) << self.id
        Event.includes(:users).where(["`users`.id IN (#{ids.join(',')})"])
      end
    
      # ...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not the best with multi-threading, and this is a bit complicated, so hopefully
This might be a bit complicated, but bear with me. I have a Windows
I'm having bit complicated object model that forms a triangle. There is User entity
This is a bit complicated so bear with me: On FORM.php, I have 2
OK this is bit complicated because my tables are not referenced or combined or
This is a bit complicated but here goes. I have a Rails app that
OK, this might sound a bit confusing and complicated, so bear with me. We've
I have this bit of javascript written with jQuery 1.2.5. It's contained inside the
I have this bit of script to widen a text box on mouseover and
I have this bit of code I found on the web at the end

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.