Ask A Question

Notifications

You’re not receiving notifications from this thread.

Dynamic Active Record Associations?

Brent C asked in General

Trying to wrap my head around the best way to handle this.

I have an Event Model that has_and_belongs_to_many :event_types
in my Event model I currently have a column for price..

However, what I want to do is have a price for each event_type for this particular Event not just one price for the Event.

So when a user creates a new Event based on the event_type selections they make (checkboxes) I would use javascript to display a new input field for each of those selections. The JS isn’t the issue, What I’m struggling with is seeing the big picture.

Do I create another model, do I need to change my has_and_belongs_to_many to a has_many through, do I make a polymorphic association? Not sure the right path and just need a point in the right direction.

Thanks!

Reply

The price is an attribute of the relation between Event and EventType. has_and_belongs_to_many doesn't allow you to add attributes, so you need to create a full model to hold the relation. I don't know what to call it; maybe just EventPrice.

So:

  • Event has_many :event_prices
  • EventPrice belongs_to :event
  • EventType has_many :event_prices
  • EventPrice belongs_to :event_type

price is then an attribute of the EventPrice model.

You can use has_many :through to reach through Event to EventTypes (and vice versa) if you need to.

Reply

I'd follow Bob's advice on this one. I was going to suggest the same thing. The main problem with has_and_belongs_to_many is that it doesn't let you access the join record easily so you need to swap it with an actual model named like EventPrice and do the has_many and belongs_to with that model.

Reply

Thanks Bob and Chris.

Bob, this was the exact clarity I was looking for -Thank you.

Chris, I am not a fan of my has_and_belongs_to_many relationship infact it has caused me pain in other areas (it was put into the project early on). However, those pressing pains have been resolved one way or another. The question now is whether or not I should refactor it and create a has_many :through? A bit of plumbing has been put into place and I will definitely need to spend sometime if I refactor. Is it worth it? Would you consider has_and_belongs_to_many almost a code smell?

Reply

I would definitely consider it a code smell. The good thing is that if you are using it, you already have a join table and you can just create a new model file to represent it and swap your associations out.

It's usually pretty easy to swap out the associations because you'll basically use the same options only this time it has a defined Rails model for the join table instead of it being generated inside Rails at runtime.

Reply

Ok, good to know. From your description it sounds relatively painless -I will make the necessary modifications. As it applies to my first question, I would then just add a column event_prices to my new Model "EventEventType" (poor naming, I know) then setup the proper associations. Sounds like a better longterm plan.

Reply

Ok, this is much better now! THANK YOU.
What should I do with the old join table? Should I create a migration to destroy it? Or is there a preferred solution?

Reply

Yeah, you could just create one to remove the table. It'll keep things cleaner.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,160+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.