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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:53:07+00:00 2026-05-22T18:53:07+00:00

I have 2 models users and events and I used a has_many :through definition

  • 0

I have 2 models “users” and “events” and I used a has_many :through definition to define a many to many relationship between users and events. Each user can belong to 0 or many events and each event can have 0 or many users associated with it.

I know when I have a has_many and belongs_to relationship, I can simply do user.events.create(arg) and rails knows that the event is associated with that given user. However, when it is many to many, how do I associate them? How do I say user X now belongs to event A and user Y belongs to event A and user X also belongs to event B. Then, additionally, how would I destroy that relationship if I wanted?

Since neither the event nor the user only belongs to a single user/event…how would I define the relationship after a user instance and event instance are already created?

So…I guess i’m asking if this works:

@user = User.create(args)
@event = Event.create(args)
@user.events << @event

does this automagically associate the user with the event I just added to the user? It seems to work but then when I try this:

@user2 = User.create(args)
@user2.events << @event

this doesn’t seem to associate @user2 with that specific event but it associates the event with @user2. I did:

@event.users

and only @user was listed…not @user2 as well.

  • 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-22T18:53:08+00:00Added an answer on May 22, 2026 at 6:53 pm

    Check out the rails guide on associations. Specifically section 4.3 has_many Association Reference

    It looks like the method you’re looking for is @customer.orders << @order1. In your case, with @user and @event, you’ll want to do the following:

    @user.events << @event
    

    and the correct associations will be created.

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

Sidebar

Related Questions

I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have two models - Tenant and User, each tenant will have_many users and
I have these models: class UserProfile(models.Model): user = models.OneToOneField(User) #etc class Organization(models.Model): users =
I have a working HABTM association between the models Posts and Users... the posts_users
I have two models -- User and Entry -- that are related through a
I have a model that looks like this: class Invite(models.Model): user = models.ForeignKey(User) event
I have 3 models sites, user_favorites and users. Relevant relationships: class Site < ActiveRecord::Base
I have two models, user and group. I also have a joining table groups_users.
I have two models: User and Teacher : class User < ActiveRecord::Base attr_accessor :password
I have two models User and Admin(with RailsAdmin) that use Devise. I sign in

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.