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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:12:15+00:00 2026-05-15T16:12:15+00:00

I have a model called Purchase and a model called TicketType. A purchase can

  • 0

I have a model called Purchase and a model called TicketType. A purchase can have many ticket types, and a ticket type can have many purchases.

So I have

class Purchase < ActiveRecord::Base
  has_many :purchases_ticket_types, :class_name => 'PurchaseTicketType'
  has_many :ticket_types, :through => :purchases_ticket_types
end

class TicketType < ActiveRecord::Base
  has_many :purchases_ticket_types, :class_name => 'PurchaseTicketType'
  has_many :purchases, :through => :purchases_ticket_types
end

class PurchaseTicketType < ActiveRecord::Base
  set_table_name "purchases_ticket_types"
  belongs_to :purchase
  belongs_to :ticket_type
end

I knew purchases_ticket_types for a table name was going to be trouble as soon as ActiveRecord failed to identify the correct table name from the camel-cased class name. I ended having to call set_table_name.

But the worst part is having to do this:

purchase = Purchase.find(1)
purchase.purchases_ticket_types.each do |purchase_ticket_type|
  puts 'join contains quantity: ' + purchase_ticket_type.quantity
  puts 'ticket type name is: ' + purchase_ticket_type.ticket_type.name
end

Look how verbose and repetitive that reads. Everything is working but it looks so ugly. Is there a better way of naming many-to-many associations to avoid this kind of thing: purchase.purchases_ticket_types.first.ticket_type.name ?

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-15T16:12:16+00:00Added an answer on May 15, 2026 at 4:12 pm

    How about using TicketSale as the join model name (or simply Sale if your application doesn’t sell anything else apart from tickets):

    class Purchase < ActiveRecord::Base
      has_many :ticket_sales
      has_many :ticket_types, :through => :ticket_sales
    end
    
    class TicketType < ActiveRecord::Base
      has_many :ticket_sales
      has_many :purchases, :through => :ticket_sales
    end
    
    class TicketSale < ActiveRecord::Base
      belongs_to :purchase
      belongs_to :ticket_type
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.