This project is archived and is in readonly mode.

#5579 ✓resolved
Robert Pankowecki

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

    Mark Mulder September 8th, 2010 @ 11:38 AM

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

    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

    Neeraj Singh September 14th, 2010 @ 05:34 AM

    Can someone verify my patch and provide comments. Thanks.

  • Lake

    Lake September 15th, 2010 @ 07:23 AM

    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

  • Neeraj Singh

    Neeraj Singh September 15th, 2010 @ 04:49 PM

    @Lake thanks for verifying.

  • Neeraj Singh

    Neeraj Singh September 19th, 2010 @ 02:34 PM

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

    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
  • Repository

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

  • Jeremy Kemper

    Jeremy Kemper October 15th, 2010 @ 11:02 PM

    • Milestone set to 3.0.2
  • Andrea Franceschini

    Andrea Franceschini December 26th, 2010 @ 01:06 PM

    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.

  • Dave Myron

    Dave Myron January 10th, 2011 @ 12:14 PM

    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?

  • Robert Pankowecki

    Robert Pankowecki January 16th, 2011 @ 07:10 PM

    @Dave - could you write a failing test case ?

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>

Attachments

Referenced by

Pages