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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:38:22+00:00 2026-05-14T02:38:22+00:00

We are creating a rails application for an already existing database. We need to

  • 0

We are creating a rails application for an already existing database. We need to map some database tables together.

Suppose we have three tables: event, event_groups and event_to_groups.
There are some events, there are some groups, and each event can be assigned to one or more groups.

How do I model this relation in rails?

eg:

Existing tables:

event
    ID      name
    --------------------
    3       dinner
    4       sport
    5       anniversary
    6       birthday

event_groups
    ID      name
    --------------------
    1       work
    2       friends
    3       family

event_to_groups
    event_id    event_groups
    --------------------
    3           2
    3           3
    4           1
    4           2
    4           3
    5           3
    6           2
class Events < ActiveRecord::Base
  set_table_name 'events'
end
class Groups < ActiveRecord::Base
  set_table_name 'groups'
end
class EventToGroups < ActiveRecord::Base
  set_table_name 'event_to_groups'
end

How can I retrieve group names belonging to an event from the event model? Thanks.

  • 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-14T02:38:23+00:00Added an answer on May 14, 2026 at 2:38 am

    A few things – first of all, the ActiveRecord model should be singular, not plural. Also, I assume you meant that the table for the Group model is ‘event_groups’, and not ‘groups’?

    Try this:

    class Event < ActiveRecord::Base
      set_table_name 'events'
      has_many :event_to_groups
      has_many :groups, :through => :event_to_groups
    end
    
    class Group < ActiveRecord::Base
      set_table_name 'event_groups'
      has_many :event_to_groups
      has_many :events, :through => :event_to_groups
    end
    
    class EventToGroup < ActiveRecord::Base
      set_table_name 'event_to_groups'
      belongs_to :event
      belongs_to :group, :foreign_key => 'event_groups'
    end
    

    I’d also choose a more descriptive model name than “EventToGroup” (maybe “Attendee” or something), but that’s up to you.

    That should do it – then you can do something like:

    @event = Event.find(5)
    @groups = event.groups
    

    Edit: Ack, those relationships should be has_many, not just many. I’ve been using MongoMapper too much.

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

Sidebar

Related Questions

I'm creating a Rails application, and I've hit a bit of a snag. I
I'm creating a fast application with ruby on rails, and after doing a lot
I have been creating a website with Ruby on Rails, and will be hosting
When creating a web application, and lets say you have a User object denoting
I'm working creating rails application and wondered where I can find good tutorials on
I'm creating rails application where it allow users to add geo-location data for restaurants,
I am creating a rails application that needs to store a large amount of
I have a Ruby on Rails (2.3.5) application and an APE (Ajax Push Engine)
I want to automate things like: Creating a new Ruby on Rails application with
I am creating a Rails plugin and it is dynamically adding a method to

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.