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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:55:32+00:00 2026-06-12T07:55:32+00:00

I have a 3 column join table that is storing 3 ID’s for 3

  • 0

I have a 3 column join table that is storing 3 ID’s for 3 different HABTM models.

Models

# ProductGrade.rb
has_and_belongs_to_many :vendors, :join_table => "item_codes_product_grades_vendors"
has_and_belongs_to_many :item_codes, :join_table => "item_codes_product_grades_vendors"

# Vendor.rb
has_and_belongs_to_many :prouduct_grades, :join_table => "item_codes_product_grades_vendors"
has_and_belongs_to_many :item_codes, :join_table => "item_codes_product_grades_vendors"

# ItemCode.rb
has_and_belongs_to_many :vendors, :join_table => "item_codes_product_grades_vendors"
has_and_belongs_to_many :product_grades, :join_table => "item_codes_product_grades_vendors"

I am simply wanting to record the 3-part association when a user updates on the Vendor model.

Vendors_Controller.rb

def update
  i = ItemCode.find(params[:vendor][:item_codes].to_i)
  i.vendors << Vendor.find(params[:id])
  i.product_grades << ProductGrade.find(params[:product_grade_id])

  redirect_to product_grade_vendor_path
end

This correctly saves the 3 columns of data in the join table, however it is creating two-different records, like this:

-- *product_grade_id* -- *vendor_id* -- *item_code_id* --
---------------------------------------------------------
--                12  --       NULL  --             4  --
--                12  --          6  --           NULL --

I realize this is probably a silly syntax issue, but I just want to know how to get the controller to save both of those values in 1 record.

Thanks for the help!

  • 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-12T07:55:34+00:00Added an answer on June 12, 2026 at 7:55 am

    Consider using has_many :through => instead of HABTM. ActiveRecord can’t address join tables in HABTM but it can address join tables in has_many :through =>. With the later option, the join table will be represented as a model and you will be given tools how to manipulate it, change it, update it, etc.

    I recommend using HABTM only when you are joining two models, but not three. Your update method will then be very much simplified.

    def update
      Association.create!(:product_grade_id => "...", :vendor_id => "...", :item_code_id => "..."
    
      redirect_to wherever_path
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model that acts as a join table called CELEBRATIONS. CELBERATION has_and_belongs_to_many
I have a table for absentees, and that table am storing the studentids of
I have a table with more than 100 columns. I need to join it
I have column with varchar values like 022008 that I need to convert into
In database table I have column called options . It has type of integer
I have table called Buttons. Buttons table i have column button_number . Table contain
I currently have a closure table used for hierarchical data that has 5 million
I have a join table where the original table is a numeric type and
I have a query with a self join that looks like this, select t1.
I have premiere_poland column in database table which is date type. /** @Column(type=date, nullable=TRUE)

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.