#653 ✓resolved
Arthur Zapparoli

[PATCH] Add first_or_initialize method

Reported by Arthur Zapparoli | November 14th, 2008 @ 10:15 PM

I added first_or_initialize method, which is the same as first_or_create, but don't save the object in the database.

Here's the commit from my fork: http://github.com/arthurgeek/dm-...

Comments and changes to this ticket

  • Arthur Zapparoli

    Arthur Zapparoli November 26th, 2008 @ 12:50 AM

    • Assigned user cleared.

    I'm attaching a patch file for that. It's the best method instead of github commits?

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) November 26th, 2008 @ 11:43 PM

    This patch probably won't be applied to sam/dm-core, which is basically just in maintenance mode while the docs and specs for dkubb/dm-core are being worked on.

    However, I would be open to having a first_or_new method in both Model and Collection, since both already (or will) have a first_or_create method.

    Also the implementation of the method can be made even simpler than what you have:

    def first_or_create(query, attributes = {})
      first(query) || new(query.merge(attributes))
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) November 27th, 2008 @ 12:05 AM

    • State changed from “new” to “resolved”
    • Assigned user set to “Dan Kubb (dkubb)”

    This has now been added as per the following commit:


    Please note that there are no specs for Model#first_or_create or Model#first_or_new because some of the "finders" in Collection will have their specs extracted into a shared spec for use in testing Model, Collection, OneToMany::Proxy and ManyToMany::Proxy.

    Also note the finders will likely be extracted into their own module for inclusion into Model and Collection to ensure the behavior is consistent between the two.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket