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 190225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:14:17+00:00 2026-05-11T16:14:17+00:00

This code is taken from a previous question, but my question directly relates to

  • 0

This code is taken from a previous question, but my question directly relates to it, so I’ve copied it here:

class User < ActiveRecord::Base
  has_many :group_memberships
  has_many :groups, :through => :group_memberships
end

class GroupMembership < ActiveRecord::Base
  belongs_to :user
  belongs_to :role
  belongs_to :group
end

class Role < ActiveRecord::Base
  has_many :group_memberships
end

class Group < ActiveRecord::Base
  has_many :group_memberships
  has_many :users, :through > :group_memberships
end

New Question Below

How you take the above code one step further and add and work with friends?

class User < ActiveRecord::Base
  has_many :group_memberships
  has_many :groups, :through => :group_memberships
  has_many :friends # what comes here?
  has_many :actions
end

In the code above I also added actions. Let’s say that the system kept track of each user’s actions on the site. How would you code this so that each action was unique and sorted with the most recent at the top for all the user’s friends?

<% for action in @user.friends.actions %>
 <%= action.whatever %>
<% end %>

The code above may not be valid, you could do something like what I have below, but then the actions wouldn’t be sorted.

<% for friend in @user.friends %>
 <% for action in friend.actions %>
  <%= action.whatever %>
 <% end %>
<% end %>

UPDATE

I guess the real issue here is how to define friends? Do I create a new model or join table that links users to other users? Ideally, I’d like to define friends through the common group memberships of other users, but I’m not sure how to go about defining that.

has_many :friends, :through => :group_memberships, :source => :user

But that doesn’t work. Any ideas or best practice suggestions?

  • 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-11T16:14:18+00:00Added an answer on May 11, 2026 at 4:14 pm

    It’s been a while since I’ve worked with Rails (think Rails 1.x), but I think you can do something like the following:

    Action.find(:all, :conditions => ['user_id in (?)', @user.friends.map(&:id)], :order => 'actions.date DESC')
    

    and then in your view:

    <% @actions.each do |action| %>
      <%= action.whatever -%>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 96k
  • Answers 96k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you have placed the User Control in separate assembly,… May 11, 2026 at 7:14 pm
  • Editorial Team
    Editorial Team added an answer You should use case #1 because it's free of case… May 11, 2026 at 7:14 pm
  • Editorial Team
    Editorial Team added an answer Don't think this is built into XPath, you probably need… May 11, 2026 at 7:14 pm

Related Questions

I would be certain that this question addresses something that would have been brought
In this question someone asked for ways to display disk usage in Linux. I'd
I have a form that uses the excellent jQuery validation plugin. This is a
We use a data acquisition card to take readings from a device that increases
Hello again ladies and gents! OK, following on from my other question on ASP.NET

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.