Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Rails3 - accepts_nested_attributes_for reject_if option does not work.

#5579

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

Reported by Robert Pankowecki · September 8th, 2010 @ 09:35 AM

State: resolved
Milestone: 3.0.2
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. Mark Mulder
    Mark Mulder

    Hmm, 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 >
    

    September 8th, 2010 @ 11:38 AM

  2. Neeraj Singh
    Neeraj Singh
    • 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.

    September 8th, 2010 @ 04:12 PM

  3. Neeraj Singh
    Neeraj Singh

    Can someone verify my patch and provide comments. Thanks.

    September 14th, 2010 @ 05:34 AM

  4. Lake
    Lake

    Hi 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

    September 15th, 2010 @ 07:23 AM

  5. Neeraj Singh
    Neeraj Singh

    @Lake thanks for verifying.

    September 15th, 2010 @ 04:49 PM

  6. Neeraj Singh
    Neeraj Singh
    • Milestone set to 3.x
    • Assigned user changed from Neeraj Singh to José Valim

    September 19th, 2010 @ 02:34 PM

  7. Neeraj Singh
    Neeraj Singh
    • Assigned user changed from José Valim to Aaron Patterson

    Assigning it to Mr. Patterson as per advice from Santiago.

    September 24th, 2010 @ 01:49 AM

  8. Neeraj Singh
  9. Repository
    Repository
    • 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...

    September 24th, 2010 @ 12:11 PM

  10. Jeremy Kemper
  11. Andrea Franceschini
    Andrea Franceschini

    Hi 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.rb

    class 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 => true
    

    end

    app/models/invoice_item.rb

    class InvoiceItem < ActiveRecord::Base

    attr_accessible :price, :discount, :description
    belongs_to :invoice
    

    end

    I could verify that the :reject_if is honoured when creating a new Invoice, while it's not when updating an existing one.

    December 26th, 2010 @ 01:06 PM

  12. Dave Myron
    Dave Myron

    The 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?

    January 10th, 2011 @ 12:14 PM

  13. Robert Pankowecki
    Robert Pankowecki

    @Dave - could you write a failing test case ?

    January 16th, 2011 @ 07:10 PM