This project is archived and is in readonly mode.
:any_blank and :all_blank options for accepts_nested_attributes_for :reject_if
Reported by Mike Breen | April 15th, 2009 @ 06:06 PM | in 2.3.6
I'm seeing a pattern in my projects when using
accepts_nested_attributes_for
with the
:reject_if
option:
class Blog < ActiveRecord::Base
has_many :posts
has_one :author
accepts_nested_attributes_for :author, :reject_if => proc { |attributes| attributes.all? {|k,v| v.blank?} }
accepts_nested_attributes_for :posts, :reject_if => proc { |attributes| attributes.any? {|k,v| v.blank?} }
end
This patch allows you to pass the values :all_blank
and :any_blank
to :reject_if
and will
generate the proc for you.
class Blog < ActiveRecord::Base
has_many :posts
has_one :author
accepts_nested_attributes_for :author, :reject_if => :all_blank
accepts_nested_attributes_for :posts, :reject_if => :any_blank
end
Comments and changes to this ticket
-
Adam McCrea April 17th, 2009 @ 02:14 PM
+1 This feels much cleaner, and I agree that these will be the most common scenarios.
-
Carlos Antonio da Silva April 17th, 2009 @ 11:59 PM
It seems reasonable for me to have this patterns, as I've been writing similar procs several times.
-
Mike Breen May 5th, 2009 @ 07:54 PM
Thanks for all the +1 love but in order for core to take this seriously you'll need to add just a little bit more.
Thanks again. Mike
-
Mike Breen May 10th, 2009 @ 03:29 AM
- Assigned user set to Michael Koziarski
After talking with Koz the :any_blank is being dropped. The thinking is that you'd be better served by validations there.
-
Mike Breen August 13th, 2009 @ 12:24 AM
Is there a reason we can't get this patch into 2-3-stable. It applies cleanly.
-
CancelProfileIsBroken August 13th, 2009 @ 12:26 AM
- State changed from resolved to open
Seems a good candidate for backport to 2-3-stable.
-
Michael Koziarski August 13th, 2009 @ 12:27 AM
- Milestone changed from 2.x to 2.3.4
-
Jeremy Kemper September 11th, 2009 @ 11:04 PM
- Milestone changed from 2.3.4 to 2.3.6
[milestone:id#50064 bulk edit command]
-
Steve Richert November 19th, 2009 @ 02:57 PM
- Tag cleared.
The
:reject_if
option receives a Proc that's called on the attributes hash. If thisall_blank
usage is important enough to integrate intoaccepts_nested_attributes_for
, why not include it in ActiveSupport's Hash extensions. Then it can be used withaccepts_nested_attributes_for
as follows:accepts_nested_attributes_for :author, :reject_if => :all_blank?.to_proc
Even better, it'd be nice to see the
:reject_if
option handle theto_proc
bit on its own. This would open:reject_if
up to very easily use whatever other fancy Hash methods are floating around out there. -
Eloy Duran December 30th, 2009 @ 07:49 PM
- Assigned user changed from Michael Koziarski to Eloy Duran
-
Repository January 7th, 2010 @ 07:32 PM
- State changed from open to resolved
(from [69db137f891e0ff5577eeab72da6600db1d2cbcf]) Allows you to pass :all_blank to :reject_if option to automatically create a Proc that will reject any record with blank attributes.
[#2501 state:resolved]
Signed-off-by: Eloy Duran eloy.de.enige@gmail.com
http://github.com/rails/rails/commit/69db137f891e0ff5577eeab72da660...
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
- 2501 :any_blank and :all_blank options for accepts_nested_attributes_for :reject_if [#2501 state:resolved]