This project is archived and is in readonly mode.

#5638 ✓committed
Nathan B

[Patch] Rails3 - accepts_nested_attributes_for reject_if => {blank id} raises ActiveRecord::RecordNotFound when id is an empty string

Reported by Nathan B | September 16th, 2010 @ 05:25 AM | in 3.0.2

This happens if there is a select list to choose an existing 'task' id, with :include_blank => true, and the user submits the form with the blank task selected.

class Project < ActiveRecord::Base
  has_many :tasks
  accepts_nested_attributes_for :tasks, :reject_if => proc { |attributes| attributes[:id].blank? } 

class Task < ActiveRecord::Base
  belongs_to :project

p = => "project", :tasks_attributes => {"0" => {"name" => "new name", "id" => ""}})

=> ActiveRecord::RecordNotFound (Couldn't find Account with ID= [WHERE (`accounts`.`deleted_at` IS NULL)]):

We traced the problem to activerecord/lib/active_record/nested_attributes.rb in the assign_nested_attributes_for_one_to_one_association method, at line 326:

elsif attributes['id']

should be

elsif not attributes['id'].blank?

(Note: assign_nested_attributes_for_collection_association uses attributes['id'].blank? already at line 395)

This is on Rails 3.0.0

Patch is attached (including test), or on my rails fork:

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=""></a>