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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:53:49+00:00 2026-05-28T04:53:49+00:00

I am new to Ruby on Rails and I’m desperately trying to get a

  • 0

I am new to Ruby on Rails and I’m desperately trying to get a HABTM association working. I have a customers database in which every customer can have many contacts. From the customers “show” view, I want to have a link to add a new contact to that selected customer.

So far, I have the following data:

models/customer.rb:

class Customer < ActiveRecord::Base
  has_and_belongs_to_many :contacts
  accepts_nested_attributes_for :contacts, :allow_destroy => true
end

models/contact.rb:

class Contact < ActiveRecord::Base
  has_and_belongs_to_many :customers
end

views/customers/show.html.erb:

<%= link_to 'Add new contact', new_customer_contact_path(@customer) %>

I also have three tables:

customers (id, name)
contacts (id, name)
contacts_customers (contact_id, customer_id)  <- the join-table

Now when I click on the “add new contact”-link, I get to a “new customer” page, it even correctly assigns the customer_id, but when I click on “save” after filling in the contact-information, the contact is created, but the join-table (contacts_customers) is not updated. As a result, the newly created contact is not “connected” to the customer.

I already browsed the net for about two hours on this one and cannot help myself. I’d be more than happy if somebody could point me in the right direction!

Thanks!

[Edit: Updated details]
Here’s which parameters get passed over once I click on the “new contact” link on my “show.html.erb” view

Started GET "/customers/260/contacts/new" for 127.0.0.1 at 2012-01-18 08:21:28
  Processing by ContactsController#new as HTML
  Parameters: {"customer_id"=>"260"}
  Customer Load (0.0ms) SELECT 'customers'.* FROM 'customers' WHERE 'customers'.'id' = 260 LIMIT 1
  Rendered contacts/_form.html.erb (9.0ms)
  Rendered contacts/new.html within layouts/application (13.0ms)
  Completed 200 OK in 343 ms (Views: 26.0ms  | ActiveRecord: 299.0ms)

As you can see, the customer_id (it is 260 in my example) should be passed over to the new contact controller.
When I click on “save” however which starts the POST-request to actually create the database entries, the customer_id
is lost and only the contact table is updated:

Started POST "/contacts" for 127.0.0.1 at 2012-01-18 08:28:00 +0100
  Processing by ContactsController#create as HTML
  Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"jWMlruMezFGOy1j7vTbE9OcL8VareGUXS1AprBpNhGA=", "contact"=>{"anrede"=>"", "name"=>"Test", "position"=>"", "telefon"=>"", "mobil"=>"", "fax"=>"", "email"=>"", "sonstiges"=>""}, "commit"=>"Create Contact"}
  BEGIN SQL (0.0ms)
  INSERT INTO `contacts` (`anrede`, `email`, `fax`, `mobil`, `name`, `position`, `sonstiges`, `telefon`) VALUES ('', '', '', '', 'Test', '', '', '') (113.0ms)
  COMMIT
  Redirected to http://localhost:3000/contacts/626
  Completed 302 Found in 174ms

I’m not sure if it’s of any help, but just in case. My routes are set up like this:

resources :contacts
resources :customers do
  resources :contacts
end
  • 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-28T04:53:50+00:00Added an answer on May 28, 2026 at 4:53 am

    One thing to check is if you defined any attributes using the macro attr_accessible. For more info look this below link.

    http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#label-Saving.

    I would suggest to test the params hash in the console. You can use a logger statement to see the params hash and try to change the hash to make it work.

    It would be helpful if you post the params hash to see what you are missing

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

Sidebar

Related Questions

I'm a new ruby/rails user trying to get my mac set up for the
I'm new to ruby on rails. So i think i have association problems. Given
I am new to Ruby on Rails I have a scenario in which I
I'm new to ruby/rails and trying to build a simple Projects / Tags app
I am new to Ruby on Rails, I have completed the Blog Tutorial .
I'm new to Ruby on Rails and I'm trying to model my table relationships.
I am new to Ruby and Rails so bear with me please. I have
I'm new to Ruby on Rails, and I'm trying to create a bass guitar
I am a new Ruby on Rails user and had a question. I have
I'm new to Ruby on rails and am a bit confused. I have an

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.