This project is archived and is in readonly mode.
Rails3 - accepts_nested_attributes_for reject_if option does not work.
-
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 > -
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.
-
Neeraj Singh
Can someone verify my patch and provide comments. Thanks.
-
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
-
Neeraj Singh
@Lake thanks for verifying.
-
Neeraj Singh
- Milestone set to 3.x
- Assigned user changed from Neeraj Singh to José Valim
-
Neeraj Singh
- Assigned user changed from José Valim to Aaron Patterson
Assigning it to Mr. Patterson as per advice from Santiago.
-
Neeraj Singh
- Milestone cleared.
-
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... -
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 => trueend
app/models/invoice_item.rb
class 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
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
@Dave - could you write a failing test case ?
