This project is archived and is in readonly mode.
[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? }
end
class Task < ActiveRecord::Base
belongs_to :project
end
p = Project.new(:name => "project", :tasks_attributes => {"0" => {"name" => "new name", "id" => ""}})
p.save
=> 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: http://github.com/ndbroadbent/rails/commit/f13a5674885f3d3388b5bc03...
Comments and changes to this ticket
-
Neeraj Singh September 16th, 2010 @ 01:14 PM
- Importance changed from to Low
Not sure how much of overlap is there between this ticket and #5579 . I will look into that later.
-
José Valim October 15th, 2010 @ 11:01 PM
- Milestone cleared.
- Assigned user set to José Valim
-
Jeremy Kemper October 16th, 2010 @ 12:49 AM
- Milestone set to 3.0.2
-
Neeraj Singh November 11th, 2010 @ 04:04 PM
- State changed from new to open
- Tag set to patched
+1
Reviewing this patch as per request from Santiago. Tested the patch and it looks good.
-
José Valim November 11th, 2010 @ 04:11 PM
- Assigned user changed from José Valim to Santiago Pastorino
Yup, it looks good. I want just to note that in Rails Code Guidelines is stated that we don't use "not". So this would probably be better written as !a["id"].blank? or a["id"].present?
-
Repository November 11th, 2010 @ 05:27 PM
- State changed from open to committed
(from [894fd288596b005148c3882e6431f6453d198101]) Fixed bug in active_record/nested_attributes where an empty string id caused an ActiveRecord::RecordNotFound error. Found by [Ben Tillman]
[#5638 state:committed]
Signed-off-by: Santiago Pastorino santiago@wyeworks.com
https://github.com/rails/rails/commit/894fd288596b005148c3882e6431f... -
Repository November 11th, 2010 @ 05:32 PM
(from [d54ab371df9eb34ce26386a6656144e81eadc4b3]) Fixed bug in active_record/nested_attributes where an empty string id caused an ActiveRecord::RecordNotFound error. Found by [Ben Tillman]
[#5638 state:committed]
Signed-off-by: Santiago Pastorino santiago@wyeworks.com
https://github.com/rails/rails/commit/d54ab371df9eb34ce26386a665614...
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
Attachments
Tags
Referenced by
- 5638 [Patch] Rails3 - accepts_nested_attributes_for reject_if => {blank id} raises ActiveRecord::RecordNotFound when id is an empty string [#5638 state:committed]
- 5638 [Patch] Rails3 - accepts_nested_attributes_for reject_if => {blank id} raises ActiveRecord::RecordNotFound when id is an empty string [#5638 state:committed]