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.

Add blocks to AR create and update

#39

This patch adds the block syntax to the ActiveRecord class methods create and update.

For example it allows this:

@person = Person.create(params[:person]) do |p|
  p.has_rails_patch = false
  p.set_status :uncool
end

@person = Person.update(params[:id], params[:person]) do |p|
  p.has_rails_patch = true
  p.set_status :cool
end 

And for the array variants of these methods it will call the block for each record:

Person.create([{:name => 'Matz'}, {:name => 'David'}]) do |p|
  p.set_status :cool
end

Both people will be set to cool status.

The create! is also patched.

Tests included.

Thanks,

Adam.

Reported by Adam Meehan · April 22nd, 2008 @ 11:24 PM

State: resolved
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Adam Meehan
    Adam Meehan

    Updated patch with conforming more to rails conventions with blocks.

    April 23rd, 2008 @ 09:27 AM

  2. Ken Miller
  3. wildchild
  4. Yaroslav Markin
  5. matthuhiggins
    matthuhiggins

    Perhaps a tangent, but why not replace

    object = new(attributes)

    yield(object) if block_given?

    object.save

    object

    with

    returning new(attributes) do |object|

    yield(object) if block_given?

    object.save

    end

    April 27th, 2008 @ 06:53 AM

  6. Adam Meehan
    Adam Meehan

    Sure that would be nice. I was not sure how often 'returning' was being used in rails code. I thought of it more as for external use.

    From greping the rails edge code I see there is some use of it. I am happy to change it to the returning convention if thats the way rails is going.

    April 27th, 2008 @ 07:24 AM

  7. Adam Meehan
    Adam Meehan

    Added another patch which uses returning. I will let the core team decide what they prefer.

    Thanks for the suggestion Matt.

    April 27th, 2008 @ 07:36 AM

  8. matthuhiggins
    matthuhiggins

    Using returning reduces performance. Since create is a common operation, my suggestion might not be worth it.

    April 27th, 2008 @ 10:38 PM

  9. Adam Meehan
    Adam Meehan

    I did wonder. I will let the core team decide based on what they know and would prefer. Both patches are there.

    April 27th, 2008 @ 10:46 PM

  10. Adam Meehan
  11. Repository
    Repository
    • State changed from new to resolved

    (from [dd120ede53eaf71dee76894998a81626b7a689fc]) Added block-setting of attributes for Base.create like Base.new already has (Adam Meehan) [#39 state:resolved]

    http://github.com/rails/rails/co...

    May 1st, 2008 @ 05:14 AM

  12. DHH
    DHH
    • State changed from resolved to new

    The create block is good stuff, but I don't think the update one warrants enough use to be included. Base#update is kinda suspect as is.

    May 1st, 2008 @ 05:15 AM

  13. DHH
    DHH
    • State changed from new to resolved

    May 1st, 2008 @ 05:15 AM

  14. Adam Meehan