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

  • Home
  • SEARCH
  • 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 403655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:14:12+00:00 2026-05-12T17:14:12+00:00

A user has_many :donations , a project has_many :donations , and a donation belongs_to

  • 0

A user has_many :donations, a project has_many :donations, and a donation belongs_to :user and belongs_to :project.

I’m looking for a sensible way to extract the projects associated with a user (through donations) into an array.

I’m currently doing:

def index
  @user = User.find params[:user_id]
  @projects = []
  @user.donations.each do |donation|
    @projects << donation.project
  end
end

I feel like I’m missing something obvious, as this seems lame. Is there a better way to do this?

Edit

I accidentally simplified this too far. A user can also be associated with a project through other models, so @projects = @user.projects isn’t going to do what I need it to.

  • 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-12T17:14:12+00:00Added an answer on May 12, 2026 at 5:14 pm
    class User < AR::Base
      has_many :donations
      has_many :projects, :through => :donations
      …
    end
    
    @user.projects
    

    should work.

    For gathering many association collections see my previous answer. You will need to adapt it to use the through associations (just treat them as normal has_masnys), but the same applies.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these tables and relationships: user has_many projects project has_many tasks task has_many
Given the following Thread belongs_to :user User has_many :threads, :through => :thread_members When I
Post :belongs_to :user User :has_many :posts In my signup workflow they draft a Post
My model: class User has_many :items end class Item belongs_to :user def self.with_color(color, is_loaded)
I have a many-to-many relationship like this: A user has_many organizations through affiliations and
Here are my models: **Resource** has_many :users, :through => :kits has_many :kits **User** has_many
I have a simple rails application were book belongs_to user and user has_many book.
User has_many :codes has_many :redemptions Code belongs_to :user has_many :redemptions Redemptions has_one :code belongs_to
I have 3 models: User has_many :questions has_many :corrections end Question has_one :correction belongs_to
I'm using a :has_many, :through association to link two models, User and Place It

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.