This project is archived and is in readonly mode.

Rails3 - accepts_nested_attributes_for reject_if option does not work.
Reported by Robert Pankowecki | September 8th, 2010 @ 09:35 AM | in 3.0.2
class Project < ActiveRecord::Base
  has_many :tasks
  accepts_nested_attributes_for :tasks, :reject_if => proc { |attributes| true } # Always reject : just to prove the case.
end
class Task < ActiveRecord::Base
  belongs_to :project
end
p = Project.create!(:name => "project")
p.tasks.create!(:name => "1")
p.tasks.create!(:name => "2")
p.reload
p.attributes = {
  "tasks_attributes" => {
    "0" => {"name"=>"new name", "_destroy"=>"0", "id"=> p.tasks.first.id},
  }
}
p.tasks
 => [#<Task id: 1, project_id: 1, name: "new name", created_at: "2010-09-08 08:26:49", updated_at: "2010-09-08 08:26:49">, #<Task id: 2, project_id: 1, name: "2", created_at: "2010-09-08 08:26:49", updated_at: "2010-09-08 08:26:49">]
The task name is changed but it should not be due to reject_if proc.
This is on Rails 3.0.0
Comments and changes to this ticket
- 
            
         Mark Mulder September 8th, 2010 @ 11:38 AMHmm, it's behaviour seems weird although I might be missing something as well. I tried your example and editing a tasks attributes seems to work, adding a new one does not: ruby-1.9.2-p0 > p = Project.create!(:name => 'project') => #<Project id: 3, name: "project", created_at: "2010-09-08 10:35:31", updated_at: "2010-09-08 10:35:31"> ruby-1.9.2-p0 > p.tasks.create!(:name => 'task 1') => #<Task id: 4, name: "task 1", project_id: 3, created_at: "2010-09-08 10:35:39", updated_at: "2010-09-08 10:35:39"> ruby-1.9.2-p0 > p.tasks_attributes = [ { :id => 4, :name => 'changed'} ] => [{:id=>4, :name=>"changed"}] ruby-1.9.2-p0 > p.save => true ruby-1.9.2-p0 > p.tasks => [#<Task id: 4, name: "changed", project_id: 3, created_at: "2010-09-08 10:35:39", updated_at: "2010-09-08 10:36:06">] ruby-1.9.2-p0 > p.tasks_attributes = [ { :name => 'new task' } ] => [{:id=>5, :name=>"new task"}] ruby-1.9.2-p0 > p.save => true ruby-1.9.2-p0 > p.tasks => [#<Task id: 4, name: "changed", project_id: 3, created_at: "2010-09-08 10:35:39", updated_at: "2010-09-08 10:36:06">] ruby-1.9.2-p0 >
- 
         Neeraj Singh September 8th, 2010 @ 04:12 PM- State changed from new to open
- Assigned user set to Neeraj Singh
- Tag changed from accepts_nested_attributes_for, reject_if to accepts_nested_attributes_for, patch, reject_if
- Importance changed from  to Low
 @Mark you are right. While creating a new record reject_if is being respected but while updating it was being ignored. Attached is a patch with test. Please try this patch and see if it works. 
- 
         Neeraj Singh September 14th, 2010 @ 05:34 AMCan someone verify my patch and provide comments. Thanks. 
- 
            
         Lake September 15th, 2010 @ 07:23 AMHi Neeraj, Your patch works well for me. I ran the tests and they pass. I also ran your patch against the OP code and the patch rejects the name change on the task, just as it is supposed to. Thanks +1 
- 
         
- 
         Neeraj Singh September 19th, 2010 @ 02:34 PM- Milestone set to 3.x
- Assigned user changed from Neeraj Singh to José Valim
 
- 
         Neeraj Singh September 24th, 2010 @ 01:49 AM- Assigned user changed from José Valim to Aaron Patterson
 Assigning it to Mr. Patterson as per advice from Santiago. 
- 
         Neeraj Singh September 24th, 2010 @ 01:49 AM- Milestone cleared.
 
- 
         Repository September 24th, 2010 @ 12:11 PM- State changed from open to resolved
 (from [097240f60215b866d24aebd02cc4159bdc6e7451]) reject_id option should be respected while using nested_attributes [#5579 state:resolved] Signed-off-by: José Valim jose.valim@gmail.com 
 http://github.com/rails/rails/commit/097240f60215b866d24aebd02cc415...
- 
         
- 
            
         Andrea Franceschini December 26th, 2010 @ 01:06 PMHi there. Though I'm using rails-3.0.3 (and of course activerecord-3.0.3) I'm facing this very same issue, exactly same behaviour. FTR, here's my sources: app/models/invoice.rbclass Invoice < ActiveRecord::Base attr_accessible :tax, :discount, :modifier, :paid, :issue_date, :payment_date, :description, :items_attributes has_many :items, :class_name => 'InvoiceItem', :dependent => :destroy accepts_nested_attributes_for :items, :reject_if => proc { |a| a[:description].blank? }, :allow_destroy => trueend app/models/invoice_item.rbclass InvoiceItem < ActiveRecord::Base attr_accessible :price, :discount, :description belongs_to :invoiceend I could verify that the :reject_if is honoured when creating a new Invoice, while it's not when updating an existing one. 
- 
            
         Dave Myron January 10th, 2011 @ 12:14 PMThe proc (or symbol to a method) given to an accept_nested_attributes_for method for a has_one association never runs (for me, on Rails 3.0.3). Did anyone give that one a test? 
- 
            
         
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
Referenced by
- 
         5638 
          [Patch] Rails3 - accepts_nested_attributes_for reject_if => {blank id} raises ActiveRecord::RecordNotFound when id is an empty string
        Not sure how much of overlap is there between this ticket... 5638 
          [Patch] Rails3 - accepts_nested_attributes_for reject_if => {blank id} raises ActiveRecord::RecordNotFound when id is an empty string
        Not sure how much of overlap is there between this ticket...
- 
         6006 
          Nested Attributes: _destroy should work independent of reject_if
        This looks to be an unintended consequence of 
https://gi... 6006 
          Nested Attributes: _destroy should work independent of reject_if
        This looks to be an unintended consequence of 
https://gi...
- 
         6227 
          Rails3 - accepts_nested_attributes_for reject_if is evaluated but ignored.
        This may very well be a duplicate of #5579 but
I'm report... 6227 
          Rails3 - accepts_nested_attributes_for reject_if is evaluated but ignored.
        This may very well be a duplicate of #5579 but
I'm report...
- 
         5579 
          Rails3 - accepts_nested_attributes_for reject_if option does not work.
        [#5579 state:resolved] 5579 
          Rails3 - accepts_nested_attributes_for reject_if option does not work.
        [#5579 state:resolved]
- 
         5706 
          add has_many test for #5579 case
        Fix for #5579 involved the code change for both has_one
a... 5706 
          add has_many test for #5579 case
        Fix for #5579 involved the code change for both has_one
a...
- 
         6227 
          Rails3 - accepts_nested_attributes_for reject_if is evaluated but ignored.
        I worked on #5579 and would like to see that this ticket
... 6227 
          Rails3 - accepts_nested_attributes_for reject_if is evaluated but ignored.
        I worked on #5579 and would like to see that this ticket
...
- 
         5706 
          add has_many test for #5579 case
        (from [4966b915fe96db73933d33176c1ea5cc53e58a22])
Fix for... 5706 
          add has_many test for #5579 case
        (from [4966b915fe96db73933d33176c1ea5cc53e58a22])
Fix for...
- 
         5706 
          add has_many test for #5579 case
        (from [2525bfc5dd255a865ae3fae77b5daff833396758])
backpor... 5706 
          add has_many test for #5579 case
        (from [2525bfc5dd255a865ae3fae77b5daff833396758])
backpor...
 Chris Mear
      Chris Mear
 Jeremy Kemper
      Jeremy Kemper
 José Valim
      José Valim
 Ludo van den Boom
      Ludo van den Boom
 Neeraj Singh
      Neeraj Singh
 Robert Pankowecki
      Robert Pankowecki