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.

:any_blank and :all_blank options for accepts_nested_attributes_for :reject_if

#2501

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

Reported by Mike Breen · April 15th, 2009 @ 06:06 PM

State: resolved
Milestone: 2.3.6
Assigned to: Eloy Duran Eloy Duran
Importance: none

Activity

  1. Adam McCrea
    Adam McCrea

    +1 This feels much cleaner, and I agree that these will be the most common scenarios.

    April 17th, 2009 @ 02:14 PM

  2. Carlos Antonio da Silva
    Carlos Antonio da Silva

    It seems reasonable for me to have this patterns, as I've been writing similar procs several times.

    April 17th, 2009 @ 11:59 PM

  3. Trent Ogren
  4. Zach Holman
  5. Davis Zanetti Cabral
  6. Mike Breen
    Mike Breen

    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

    May 5th, 2009 @ 07:54 PM

  7. Mike Breen
    Mike Breen
    • 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.

    May 10th, 2009 @ 03:29 AM

  8. Michael Koziarski
    Michael Koziarski
    • State changed from new to resolved

    9010ed2

    May 10th, 2009 @ 04:03 AM

  9. Mike Breen
    Mike Breen

    Is there a reason we can't get this patch into 2-3-stable. It applies cleanly.

    August 13th, 2009 @ 12:24 AM

  10. CancelProfileIsBroken
    CancelProfileIsBroken
    • State changed from resolved to open

    Seems a good candidate for backport to 2-3-stable.

    August 13th, 2009 @ 12:26 AM

  11. Michael Koziarski
    Michael Koziarski
    • Milestone changed from 2.x to 2.3.4

    August 13th, 2009 @ 12:27 AM

  12. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.4 to 2.3.6

    [milestone:id#50064 bulk edit command]

    September 11th, 2009 @ 11:04 PM

  13. Steve Richert
    Steve Richert
    • Tag cleared.

    The :reject_if option receives a Proc that's called on the attributes hash. If this all_blank usage is important enough to integrate into accepts_nested_attributes_for, why not include it in ActiveSupport's Hash extensions. Then it can be used with accepts_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 the to_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.

    November 19th, 2009 @ 02:57 PM

  14. Eloy Duran
    Eloy Duran
    • Assigned user changed from Michael Koziarski to Eloy Duran

    December 30th, 2009 @ 07:49 PM

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

    January 7th, 2010 @ 07:32 PM

  16. Jeff Kreeftmeijer
  17. Aditya Sanghi
    Aditya Sanghi

    Automatic cleanup of spam.

    October 12th, 2010 @ 10:14 AM