Hi all, I just wanted some feedback on a AR hack I have been playing with which implements the block syntax for create and update method. For example, instead of: @person = Person.new(params[:person]) @person.has_rails_patch = false @person.set_status :uncool @person.save You could do: @person = Person.create(params[:person]) do |p| p.has_rails_patch = false p.set_status(:uncool) end And the update version @person = Person.update(params[:id], params[:person]) do |p| p.set_status(:cool) end It makes it more Rubyish with the blocks, don''t you think? The bang! versions could added easily enough, for create anyway. Let me know what you think before I put a patch in. Thanks, Adam. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
On Mon, Apr 21, 2008 at 11:18 PM, Adam Meehan <adam.meehan@gmail.com> wrote:> I just wanted some feedback on a AR hack I have been playing with > which implements the block syntax for create and update method. > > For example, instead of: > > @person = Person.new(params[:person]) > @person.has_rails_patch = false > @person.set_status :uncool > @person.save > > You could do: > > @person = Person.create(params[:person]) do |p| > p.has_rails_patch = false > p.set_status(:uncool) > end > > And the update version > > @person = Person.update(params[:id], params[:person]) do |p| > p.set_status(:cool) > end > > It makes it more Rubyish with the blocks, don''t you think? The bang! > versions could added easily enough, for create anyway. > > Let me know what you think before I put a patch in.I like it. Note that new takes a block, so you can already @person = Person.new(params[:person]) do |p| p.has_rails_patch = false p.set_status :halfway end.save! if you like. Best, jeremy --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
On Apr 21, 11:18 pm, Adam Meehan <adam.mee...@gmail.com> wrote: [snip]> You could do: > > @person = Person.create(params[:person]) do |p| > p.has_rails_patch = false > p.set_status(:uncool) > endI like this a lot. I always thought create should work that way. K --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Sounds like it should be! Go for the patch! regards, Jan On 22 Apr 2008, at 08:18, Adam Meehan wrote:> > Hi all, > > I just wanted some feedback on a AR hack I have been playing with > which implements the block syntax for create and update method. > > For example, instead of: > > @person = Person.new(params[:person]) > @person.has_rails_patch = false > @person.set_status :uncool > @person.save > > You could do: > > @person = Person.create(params[:person]) do |p| > p.has_rails_patch = false > p.set_status(:uncool) > end > > And the update version > > @person = Person.update(params[:id], params[:person]) do |p| > p.set_status(:cool) > end > > It makes it more Rubyish with the blocks, don''t you think? The bang! > versions could added easily enough, for create anyway. > > Let me know what you think before I put a patch in. > > Thanks, > Adam. > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks all. I will patch and let you know the link for the +1''s! Adam On Apr 23, 3:38 am, Jan De Poorter <j...@openminds.be> wrote:> Sounds like it should be! Go for the patch! > > regards, > Jan > > On 22 Apr 2008, at 08:18, Adam Meehan wrote: > > > > > Hi all, > > > I just wanted some feedback on a AR hack I have been playing with > > which implements the block syntax for create and update method. > > > For example, instead of: > > > @person = Person.new(params[:person]) > > @person.has_rails_patch = false > > @person.set_status :uncool > > @person.save > > > You could do: > > > @person = Person.create(params[:person]) do |p| > > p.has_rails_patch = false > > p.set_status(:uncool) > > end > > > And the update version > > > @person = Person.update(params[:id], params[:person]) do |p| > > p.set_status(:cool) > > end > > > It makes it more Rubyish with the blocks, don''t you think? The bang! > > versions could added easily enough, for create anyway. > > > Let me know what you think before I put a patch in. > > > Thanks, > > Adam.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Actually there is another thing I wanted canvas. With create and update you have the option to pass in an array to save multiple records. Using the block syntax you could call the block on each record in the array. Person.create([{:name => ''Adam''}, {:name => ''Jeremy''}]) do |p| p.set_status(:cool) end It could be powerful but is it intuitive or should they be mutually exclusive? Adam. On Apr 23, 5:54 am, Adam Meehan <adam.mee...@gmail.com> wrote:> Thanks all. I will patch and let you know the link for the +1''s! > > Adam > > On Apr 23, 3:38 am, Jan De Poorter <j...@openminds.be> wrote: > > > Sounds like it should be! Go for the patch! > > > regards, > > Jan > > > On 22 Apr 2008, at 08:18, Adam Meehan wrote: > > > > Hi all, > > > > I just wanted some feedback on a AR hack I have been playing with > > > which implements the block syntax for create and update method. > > > > For example, instead of: > > > > @person = Person.new(params[:person]) > > > @person.has_rails_patch = false > > > @person.set_status :uncool > > > @person.save > > > > You could do: > > > > @person = Person.create(params[:person]) do |p| > > > p.has_rails_patch = false > > > p.set_status(:uncool) > > > end > > > > And the update version > > > > @person = Person.update(params[:id], params[:person]) do |p| > > > p.set_status(:cool) > > > end > > > > It makes it more Rubyish with the blocks, don''t you think? The bang! > > > versions could added easily enough, for create anyway. > > > > Let me know what you think before I put a patch in. > > > > Thanks, > > > Adam.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
> Person.create([{:name => ''Adam''}, {:name => ''Jeremy''}]) do |p| > > p.set_status(:cool) > end[''Adam'', ''Jeremy''].each do |name| Person.create do |p| p.name = name p.set_status(:cool) end end is not that different, and just uses a common ruby idiom Cheers, Ian --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Effectively the same, but because the array variants of create and update already exist it is a matter whether to pass the block to each record in the array. It doesn''t have to but I was wondering if it would cause confusion. But on thinking it through I think it makes sense to me. Adam On Apr 23, 7:39 am, "Ian White" <ian.w.wh...@gmail.com> wrote:> > Person.create([{:name => ''Adam''}, {:name => ''Jeremy''}]) do |p| > > > p.set_status(:cool) > > end > > [''Adam'', ''Jeremy''].each do |name| > Person.create do |p| > p.name = name > p.set_status(:cool) > end > end > > is not that different, and just uses a common ruby idiom > > Cheers, > Ian--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Patch is in. Please check it out and see if you like enough some +1''s. http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/39-add-blocks-to-ar-create-and-update It will add this goodness: people = [{:name => ''Jeremy''}, {:name => ''Ken''}, {:name => ''Jan''}, {:name => ''Ian''}] Person.create(people) do |p| p.set_status :cool p.thanked = true end Cheers, Adam --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---