Weaz
Weaz

Reputation: 87

Ruby on Rails 3: HABTM association does not update join-table

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

Upvotes: 2

Views: 2144

Answers (1)

naren
naren

Reputation: 937

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

Upvotes: 2

Related Questions