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

  • SEARCH
  • Home
  • 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 460133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:50:33+00:00 2026-05-12T22:50:33+00:00

I can’t seem to wrap my head around this, so I thought I’d post

  • 0

I can’t seem to wrap my head around this, so I thought I’d post and see if anyone could help me out (please pardon the question if it’s insultingly simple: it’s complicated to me right now!)

I have these models:

order
service
customer

I think they speak for themselves: a service is what the customer buys when they place an order.

Ok.

So, naturally, I setup these relationships:

# a customer can have many orders
class Customer
   has_many :orders
end

# an order belongs to a single customer and can have many services
class Order
   belongs_to :customer
   has_many :services
end

… but here’s where I trip up:

# a service can belong to many orders
class Service
   # belongs_to :order ???
end

Because my understanding of belongs_to is that–if I put it there–a service could only belong to one order (it would have only one value in the order_id key field–currently not present–tying it to only one order, where it needs to be able to belong to many orders).

What am I missing here?

  • 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-12T22:50:34+00:00Added an answer on May 12, 2026 at 10:50 pm

    There are two ways to handle this. The first is a rails-managed many-to-many relationship. In this case, you use a “has_and_belongs_to_many” relationship in both the Order and Service models. Rails will automatically create a join table which manages the relationships. The relationships look like this:

    class Order
      has_and_belongs_to_many :services
    end
    
    class Service
      has_and_belongs_to_many :orders
    end
    

    The second way is to manage the join table yourself through an intermediate model. In this case, you might have another model called “LineItem” that represents a Service in the context of an Order. The relationships look like this:

    class LineItem
      belongs_to :order
      belongs_to :service
    end
    
    class Order
      has_many :line_items
    end
    
    class Service
      has_many :line_items
    end
    

    I prefer the second myself. It’s probably just me, but I don’t get as confused about what’s going on when it’s explicit. Plus if I ever want to add some attributes to the relationship itself (like perhaps a quantity in your case) I’m already prepared to do that.

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

Sidebar

Related Questions

Can anyone help me trying to find out why this doesn't work. The brushes
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can anybody help me? What should be the datatype for this type -07:00:00 of
Can't figure out how to do this in a pretty way : I have
can anyone tell me why this doesn't work? db = openOrCreateDatabase(database.db, SQLiteDatabase.CREATE_IF_NECESSARY, null); db.setLocale(Locale.getDefault());
Can't work out a way to make an array of buttons in android. This
Can anyone help me with an excel formula that groups related rows and creates
Does anyone know how can I replace this 2 symbol below from the string
Can anyone please send me the script to delete the old backup files greater
Can you cast a List<int> to List<string> somehow? I know I could loop through

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.