Save 36% for Black Friday! Learn more

Bob Showalter


20 Experience
0 Lessons Completed
0 Questions Solved


Chris, great stuff, thanks. Note that \w in a regex *is* a character class, so it does not need to be enclosed in the [ ] brackets if it's by itself. So /@(\w+)/ is all you need. Also, this would need some tweaking to avoid matching the domain name of an email address, for example. (html-pipeline's version does this)

Posted in Dynamic Active Record Associations?

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.


  • 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.

logo Created with Sketch.

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

© 2022 GoRails, LLC. All rights reserved.