What's New in Edge Rails: find_or_initialize_by_...

Posted by ryan
at 4:58 PM on Monday, June 26, 2006

Parallel to the find_or_create_by_X method that will find the requested model if it exists or create it and return it if it doesn’t is the new find_or_initialize_by_X which will find the requested model or create, but not save it, if it doesn’t exist.

The associated unit tests show us that an object initialized by this method is not yet saved to the database whereas the create version is:

assert Company.find_or_initialize_by_name("38signals").new_record?

assert !Company.find_or_create_by_name("38signals").new_record?

tags: rails, rubyonrails

Comments

Leave a response

  1. JoeyJune 18, 2006 @ 02:19 PM
    Look, I did this ages ago: http://dev.rubyonrails.org/ticket/3878
  2. Dave MyronJune 18, 2006 @ 04:46 PM
    They should've kept the verbage the same as the association collection verbage and used find_or_build_by_X
  3. Ryan DaigleJune 18, 2006 @ 04:46 PM
    You sure did, Joey. They usually reference the supporting patch submission in the commit message - don't know why they didn't this time...