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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:25:58+00:00 2026-05-14T15:25:58+00:00

1)A user can have many causes and a cause can belong to many users.

  • 0

1)A user can have many causes and a cause can belong to many users.

2)A user can have many campaigns and campaigns can belong to many users. Campaigns belong to one cause.

I want to be able to assign causes or campaigns to a given user, individually. So a user can be assigned a specific campaign. OR a user could be assigned a cause and all of the campaigns of that cause should then be associated with a user.

Is that possible? And could I set it up so that the relationships could be simplified like so:

User.causes = all causes that belong to a user

User.campaigns = all campaigns that belong to user whether through a cause association or campaign association

  • 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-14T15:25:58+00:00Added an answer on May 14, 2026 at 3:25 pm

    I believe you should use the following:

    class User < ActiveRecord::Base
       has_and_belongs_to_many :causes
       has_and_belongs_to_many :campaigns
    end
    
    class Cause < ActiveRecord::Base
       has_and_belongs_to_many :users
       has_many :campaigns
    end
    
    class Campaign < ActiveRecord::Base
       has_and_belongs_to_many :users
       belongs_to :cause
    end
    

    This way you can use

    User.causes
    User.campaigns
    
    Cause.campaing
    Cause.users
    
    Campaign.users
    Campaign.cause
    

    You can read here about has_and_belongs_to_many relationship, here about has_one and here about belongs_to.

    Let me know if this is what you want :]

    Edit:

    “I would still need User.campaigns to
    be campaigns from a user’s causes or
    individual campaigns associated with a
    user”

    You can have a method on users model that returns all campaigns. Something like this:

    def all_campaigns
       self.campaigns + self.causes.collect{ |c| c.campaigns }
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, users and hra. A user can only have one record
Take tables: User, Comment, Snippet. A User can have many Snippets. A Snippet can
I created 3 tables: User User-Group Group Where I can have a many-to-many relationship.
I have two model: User, Article A user can like or dislike many articles,
I have a textfield where the user can type what ever he want into
I have a Qt program with many buttons, user-interactable widgets, etc. At one stage
I have the following relation in my model: A user can have several consumers
Imagine that HTML page is a game surface (see picture). User can have n
If I have form where user can upload files via Ajax (and upload is
I have a situation in which user can single tap a control, which show

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.