This project is archived and is in readonly mode.

#4395 ✓resolved
Mathieu Arnold

Bug in nested_attributes when the primary key is not "id"

Reported by Mathieu Arnold | April 14th, 2010 @ 04:48 PM

In 2ff5f38abb4a44ed5356c34b40d30d446fb63408 a bug was introduced on line 357, in the case the primary key is not called "id".

Example :

create_table 'customer' do |t|
  t.string :name
end
create_table 'invoice', :primary_key => 'id_invoice' do |t|
  t.integer :client_id
  t.string :number
end
class Customer < ActiveRecord::Base
  has_many :invoices
  accepts_nested_attributes_for :invoices
end
class Invoice < ActiveRecord::Base
  set_primary_key 'id_invoice'
  belongs_to :customer
end

> @customer = Customer.create
> @customer.invoices.create
> @customer.update_attributes({invoices_attributes => {"0" => {"id" => "1", "number" => "1523"}}})

will fail because the query generated will be :

SELECT invoices.* FROM invoices WHERE invoice.client_id = 1 AND invoice.id IN ('1')

which is wrong, it should be invoice.id_invoice.

Changing line 357 to do

association.find(attribute_ids)

and not do a all(:conditions ...).

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Referenced by

Pages