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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:21:50+00:00 2026-06-07T07:21:50+00:00

(Ruby/Rails guru’s preferably :P) I’ve got a bit of an interesting question. Hope it’s

  • 0

(Ruby/Rails guru’s preferably :P)

I’ve got a bit of an interesting question. Hope it’s not already answered (wait, yes I do!) but I’ve looked and couldn’t find it. (Hopefully not because it’s not possible)

I’ve got two classes (and I’d like to keep it that way) Group and Event (see below).

class Group < ActiveRecord::Base
    has_and_belongs_to_many :events
end

However in my join table (group_events), I have additional columns which provide extenuating circumstances to the event… I want this information to be available on the event object. (For example, whether attendance is mandatory or not etc)

My second slightly related question is, can I not do the following:

class Event < ActiveRecord::Base
    has_and_belongs_to_many :groups
end

class GroupEvent < Event
    # Implies that a GroupEvent would be an Event, with all the other attributes of the group_events table minus the
    # two id's (group_id, event_id) that allowed for its existence (those would just be references to the group and event object)
end
  • 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-06-07T07:21:52+00:00Added an answer on June 7, 2026 at 7:21 am

    I would first rewrite the has_and_belongs_to_many, explicitly describing the relationship between Event and the model for GroupEvent.

    class Group < ActiveRecord::Base
      has_many :group_events
      has_many :events, :through => :group_events
    end
    
    class Event < ActiveRecord::Base
      has_many :group_events
      has_many :groups, :through => :group_events
    end
    
    class GroupEvent < ActiveRecord::Base
      belongs_to :group
      belongs_to :event
    end
    

    You can then methods within the Event class to refer to the GroupEvent attributes your’e after. For some boolean :attendance_mandatory attribute in GroupEvent, you could do something like

    class Event < ActiveRecord::Base
      has_many :group_events
      has_many :groups, :through => :group_events
    
      def attendance_mandatory?(group)
        group_events.find(group.id).attendance_mandatory?
      end
    end
    

    With some Event as e and associated Group as g, you could now do

    e.attentdance_mandatory?(g)
    

    As for your 2nd question, I think you’re looking for a piece of what I’ve posted in the first code block above.

    class GroupEvent < ActiveRecord::Base
      belongs_to :group
      belongs_to :event
    end
    

    Every table containing data you wish to interact with should have a representative model in your application. The above meets your stated criteria (exposes the attributes of a GroupEvent).

    Note: Your syntax of class GroupEvent < Event is used for Single Table Inheritance (you’d move the attributes like attendance_mandatory into the events table, and use that events table for both a regular Event and a GroupEvent – though this is outside of the scope of this question)

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

Sidebar

Related Questions

This is not a ruby/rails project deploy. I have the following situation and I
I'm new in Ruby/Rails, so, my question: what is the perfect way to upload
I'm looking for good ruby/rails oriented twitter accounts to follow. Interesting in news/events in
I'm a new Ruby/Rails guy. Here's one question puzzling me: Can we find the
I'm brand new to Ruby/Rails/RSpec, and hopefully this is a dumb/simple question. I'm currently
I have a short question on ruby / rails method naming conventions or good
I've got an existing production Ruby/Rails app that I want to migrate to MongoDB
While I'm not a complete Ruby/Rails newb, I'm still pretty green and I'm trying
Not sure how to do this in Ruby/Rails only thing I could come up
Im a newish Ruby/Rails developer with years of Java experience. This security question is

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.