This project is archived and is in readonly mode.

#39 ✓resolved
Adam Meehan

Add blocks to AR create and update

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

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.

Comments and changes to this ticket

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>

Pages