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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:32:18+00:00 2026-06-03T20:32:18+00:00

I am setting up a many to many relationship using the :through method and

  • 0

I am setting up a many to many relationship using the :through method and want to test that I have set everything up correctly.

class MenuItem < ActiveRecord::Base
  belongs_to :products #burger
  belongs_to :additions #extra mustard
end

Products would be something like hamburger

class Product < ActiveRecord::Base
  has_many :menu_items
  has_many :additions, through: :menu_items
end

additions would be something like extra mustard or pickles

class Addition < ActiveRecord::Base
  has_many :menu_items
  has_many :products, through: :menu_items
end

I have set up my fixtures so that a hamburger should have 2 additions associated with it. Now I want to test that the association works. Not exactly sure how to do this. I tried this:

in the fixture I set hamburger to have an id of 22. also set the burger to have mustard and pickles (2 additions).

test "product 22 should have 2 additions associated with it" do 
  menu_item = Product.find(22).additions
  assert_equal menu_item.count, 2 
end

I get an uninitialized constants error

NameError: uninitialized constant Product::Additions

I am sure that I am just misunderstanding something. Would really appreciate any pointers.

  • 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-06-03T20:32:21+00:00Added an answer on June 3, 2026 at 8:32 pm

    The error is because you need to use singular for belongs_to.

    class MenuItem < ActiveRecord::Base
      belongs_to :product #burger
      belongs_to :addition #extra mustard
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have many settings that I want to store in a cookie, should
I have 2 entities, A and B that have a many to many relationship.
I have a DataMapper many to many relationship, friends, that needs to be kept
I have a simple many-to-many relationship between the User class and the Place class.
I have a Linq to SQL class. There is a one to many relationship
I am setting up a has_many :through relationship on my Rails 3.2 app. I
I'm setting up a database using PHPMyAdmin and many fields will be large chunks
Let's say I have two objects: Articles and Categories with a many-many relationship between
I have a one-to-many relationship between Part and Params (a Part has many Params).
Simple question: does it matter which side of a bidirectional many-to-many relationship you set

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.