when you use a find_or_create_ automatic method with an initializer block, the block isn''t called on the first run, but is called on subsequent runs: u = User.find_or_create_by_name(''bob'') { |new_user| new_user.password = ''1234'' } u.name>> "bob"u.password>> nilu2 = User.find_or_create_by_name(''billy'') { |new_user| new_user.password = ''1234'' } u2.name>> ''billy''u2.password>> ''1234''This appears to have been around for a while. Bug filed (with patch) here: http://rails.lighthouseapp.com/projects/8994/tickets/1224-find_or_create_by_-methods-do-not-execute-their-block-on-first-call#ticket-1224-1 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Is this not intended? My understanding was always that if a record is found, no changes are made, otherwise we create one with the prams passed in and set it up with the block too. i.e. User.find_or_create_by_name(name){|new_user| new_user.password = ''1234'' } should be equivalent to: User.find_by_name(name) || User.create!{ |new_user| new_user.password = ''1234'' } Perhaps a documentation patch would be more appropriate? -Tom On 24 Oct 2008, at 17:32, Ken Miller wrote:> > > when you use a find_or_create_ automatic method with an initializer > block, the block isn''t called on the first run, but is called on > subsequent runs: > > u = User.find_or_create_by_name(''bob'') { |new_user| new_user.password > = ''1234'' } > u.name >>> "bob" > u.password >>> nil > > u2 = User.find_or_create_by_name(''billy'') { |new_user| > new_user.password = ''1234'' } > u2.name >>> ''billy'' > u2.password >>> ''1234'' > > This appears to have been around for a while. > > Bug filed (with patch) here: > > http://rails.lighthouseapp.com/projects/8994/tickets/1224-find_or_create_by_-methods-do-not-execute-their-block-on-first-call#ticket-1224-1 > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Robert Zotter
2008-Oct-24 19:09 UTC
Re: find_or_create_ methods with a block don''t always work
This seems to work in Rails 2.1.1, which version are you running? Perhaps you can try using a returning block user = returning User.find_or_create_by_name(''bob'') do |u| u.password = ''1234'' end On Oct 24, 9:32 am, Ken Miller <ken.mil...@gmail.com> wrote:> when you use a find_or_create_ automatic method with an initializer > block, the block isn''t called on the first run, but is called on > subsequent runs: > > u = User.find_or_create_by_name(''bob'') { |new_user| new_user.password > = ''1234'' } > u.name > > >> "bob" > u.password > >> nil > > u2 = User.find_or_create_by_name(''billy'') { |new_user| > new_user.password = ''1234'' } > u2.name > > >> ''billy'' > u2.password > >> ''1234'' > > This appears to have been around for a while. > > Bug filed (with patch) here: > > http://rails.lighthouseapp.com/projects/8994/tickets/1224-find_or_cre...--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Michael Koziarski
2008-Oct-24 19:38 UTC
Re: find_or_create_ methods with a block don''t always work
On Fri, Oct 24, 2008 at 6:32 PM, Ken Miller <ken.miller@gmail.com> wrote:> > > when you use a find_or_create_ automatic method with an initializer > block, the block isn''t called on the first run, but is called on > subsequent runs: > > u = User.find_or_create_by_name(''bob'') { |new_user| new_user.password > = ''1234'' } > u.name >>> "bob" > u.password >>> nil > > u2 = User.find_or_create_by_name(''billy'') { |new_user| > new_user.password = ''1234'' } > u2.name >>> ''billy'' > u2.password >>> ''1234'' > > This appears to have been around for a while. > > Bug filed (with patch) here: > > http://rails.lighthouseapp.com/projects/8994/tickets/1224-find_or_create_by_-methods-do-not-execute-their-block-on-first-call#ticket-1224-1Applied, nice catch. If it needs backporting to any other branches, let us know.> > > >-- Cheers Koz --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ken Miller
2008-Oct-24 21:15 UTC
Re: find_or_create_ methods with a block don''t always work
We found this behavior in 2.1.0, so it would be great to have it backported to 2.1.x, but I haven''t tried the patch against the that line. Looks like it might work. K On Oct 24, 12:38 pm, "Michael Koziarski" <mich...@koziarski.com> wrote:> On Fri, Oct 24, 2008 at 6:32 PM, Ken Miller <ken.mil...@gmail.com> wrote: > > > when you use a find_or_create_ automatic method with an initializer > > block, the block isn''t called on the first run, but is called on > > subsequent runs: > > > u = User.find_or_create_by_name(''bob'') { |new_user| new_user.password > > = ''1234'' } > > u.name > >>> "bob" > > u.password > >>> nil > > > u2 = User.find_or_create_by_name(''billy'') { |new_user| > > new_user.password = ''1234'' } > > u2.name > >>> ''billy'' > > u2.password > >>> ''1234'' > > > This appears to have been around for a while. > > > Bug filed (with patch) here: > > >http://rails.lighthouseapp.com/projects/8994/tickets/1224-find_or_cre... > > Applied, nice catch. > > If it needs backporting to any other branches, let us know. > > > > -- > Cheers > > Koz--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
John Barnette
2008-Oct-24 21:35 UTC
Re: find_or_create_ methods with a block don''t always work
On Fri, Oct 24, 2008 at 2:15 PM, Ken Miller <ken.miller@gmail.com> wrote:> On Oct 24, 12:38 pm, "Michael Koziarski" <mich...@koziarski.com> > wrote: >> On Fri, Oct 24, 2008 at 6:32 PM, Ken Miller <ken.mil...@gmail.com> wrote: >> >> > when you use a find_or_create_ automatic method with an initializer >> > block, the block isn''t called on the first run, but is called on >> > subsequent runs:Is this really the desired behavior? I''ve always considered that block equivalent to the AR::Base initializer: Foo.new do |f| f.thing = default_value end If that''s the case, I feel like it *should* be ignored when find_or_create_by_n returns a preexisting record. ~ j. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ken Miller
2008-Oct-24 22:07 UTC
Re: find_or_create_ methods with a block don''t always work
On Oct 24, 2:35 pm, "John Barnette" <jbarne...@gmail.com> wrote:> On Fri, Oct 24, 2008 at 2:15 PM, Ken Miller <ken.mil...@gmail.com> wrote: > > On Oct 24, 12:38 pm, "Michael Koziarski" <mich...@koziarski.com> > > wrote: > >> On Fri, Oct 24, 2008 at 6:32 PM, Ken Miller <ken.mil...@gmail.com> wrote: > > >> > when you use a find_or_create_ automatic method with an initializer > >> > block, the block isn''t called on the first run, but is called on > >> > subsequent runs: > > Is this really the desired behavior? I''ve always considered that block > equivalent to the AR::Base initializer: > > Foo.new do |f| > f.thing = default_value > end > > If that''s the case, I feel like it *should* be ignored when > find_or_create_by_n returns a preexisting record.Sorry, I left that bit of context out of my original message. In *both* of my examples, the record does NOT exist. That is, the first time the method is called, if the record exists, there''s no bug -- the block isn''t called and shouldn''t be. But if, on that first call, the record doesn''t exist, the block is still not called, which is a bug. This is related to the way AR auto-defines these methods the first time they''re invoked. 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 -~----------~----~----~----~------~----~------~--~---