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

Related Questions

Is this possible to do the following in ExpressionEngine: (code taken from here )
From my previous question for selecting specific html text, I have gone through this
I'm having hard time understanding the following C# code. This code was taken from
So I have this code that takes care of command acknowledgment from remote computers,
take a look at this example code: public class Comment { private Comment() {
This code is from Prototype.js . I've looked at probably 20 different tutorials, and
I know this is more heavy question, but I think its interesting too. It
Based on a previous question and with a lot of help from Damir Sudarevic
I have the following code (taken from actual script): $result=mysql_query($sql); while($row=mysql_fetch_assoc($result)) { $auth_token=$row['oauth_token']; $auth_token_secret=$row['oauth_token_secret'];
In my computer this code takes 17 seconds (1000 millions times): static void Main(string[]

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.