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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:41:43+00:00 2026-05-27T05:41:43+00:00

I have an Item model class Item < ActiveRecord:Base has_and_belongs_to_many :orders end And an

  • 0

I have an

Item model

class Item < ActiveRecord:Base
  has_and_belongs_to_many :orders
end

And an Order model

class Order < ActiveRecord:Base
   has_and_belongs_to_many : items
end

An order can have many items, which the HABTM will take care of. But where/how do I store the quantity of the items that are being ordered?

Eg:
Lets say Order1 has Item1 and Item2 in it. Now i want to store the quantity associated with the items like Order1 has two Item1 and five Item2.

What is the rails way to do this?

  • 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-27T05:41:44+00:00Added an answer on May 27, 2026 at 5:41 am

    One way you could do it would be to use a has_many :through association. This creates an independent entity for your join table between orders and items. In your case:

    class Order < ActiveRecord::Base
      has_many :invoices
      has_many :items, :through => :invoices
    end
    
    class Invoice < ActiveRecord::Base
      belongs_to :order
      belongs_to :items
    
      #Put in the migration for this table a column for quantity
    end
    
    class Item < ActiveRecord::Base
      has_many :invoices
      has_many :orders, :through => :invoices
    end
    

    This would solve your problem I believe. That way Order1 which is associated with Item1 would have a quantity of 2 in the Invoice table and the same with Item2 having a separate quantity of 5 in the Invoice table.

    You can read more about these relationships in the The has_many :through Association section in A Guide to Active Record Associations.

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

Sidebar

Related Questions

I have the following model: class item < ActiveRecord::Base has_one :price end class price
We have a simple item and order model. orders(url) items(title, orders_count) Where url can
I have a simple model class Ad < ActiveRecord::Base has_many :ad_items end class AdItem
I have some models like these: class Alpha < ActiveRecord::Base has_many :items end class
I have the following models: class Post < ActiveRecord::Base has_and_belongs_to_many :countries end class User
I have a datastore model representing items in an ecommerce site: class Item(db.Model): CSIN
I have two models. order and line_item. class Order < ActiveRecord::Base has_many :line_items has_many
I have a model with a relationship like this: class Category < ActiveRecord::Base has_many
I have 4 models: transac, transac_data, item, dvd_details class Transac < ActiveRecord::Base has_many :transac_datas
I have these two models class Invoice < ActiveRecord::Base has_many :items accepts_nested_attributes_for :items ...

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.