findOrCreateBy or findOrInitializeBy

View: New views
6 Messages — Rating Filter:   Alert me  

findOrCreateBy or findOrInitializeBy

by Brandon-64 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Everyone,  I think creating a dynamic findOrCreateBy or findOrInitializeBy method would be a nice addition to GORM.  Was wondering if you guys had thoughts on it? If you like I'll do the work and generate a pull request.

Brandon

Re: findOrCreateBy or findOrInitializeBy

by Graeme Rocher-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Can you describe the semantics of the methods with some examples

On Mon, Oct 26, 2009 at 7:33 PM, Brandon Leonardo
<brandon.leonardo@...> wrote:
> Hi Everyone,  I think creating a dynamic findOrCreateBy or
> findOrInitializeBy method would be a nice addition to GORM.  Was wondering
> if you guys had thoughts on it? If you like I'll do the work and generate a
> pull request.
> Brandon



--
Graeme Rocher
Head of Grails Development
SpringSource - Weapons for the War on Java Complexity
http://www.springsource.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: findOrCreateBy or findOrInitializeBy

by Robert Fischer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I've got this on my todo list for GORM Labs.  Had the start of a prototype, but it never got quite
finished.

Foo.findOrCreateByBar(bar)
meant
Foo.findByBar(bar) ?: new Foo(bar:bar).save()


I also had a more generic but analogous "findOrCreateWhere" (which actually provided the
implementation for all the findOrCreateByXXX methods).

~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Grails Expert Retainer Services
http://smokejumperit.com/grails-retainer/


Brandon Leonardo wrote:
> Hi Everyone,  I think creating a dynamic findOrCreateBy or
> findOrInitializeBy method would be a nice addition to GORM.  Was
> wondering if you guys had thoughts on it? If you like I'll do the work
> and generate a pull request.
>
> Brandon

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: findOrCreateBy or findOrInitializeBy

by Brandon-64 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

findOrCreateBy... will act basically as a dynamic finder. Up to two different fields to look by.

For example, if you're creating a FB app and you just want to create your own user object based on the FB userid:


def user = User.findOrCreateByFbUserId(params.fb_sig_user)

The method will look for that item, and return it if it is found.  If a user with that specific fbUserId can't be found then it will instantiate a new user with that fbUserId, save it, and return the newly created user.

------------

Find or initialize by would work similarly:

def user = User.findOrInitializeByFbUserId(params. fb_sig_user)

In this case, it would look for that user, and if it can't find one, then return a newly constructed item that has NOT been saved yet.

-------

Up to this point I've just created static methods on the classes to do this manually, but it seems fairly straightforward to implement, although I could be wrong.  I just didn't want to fork it and dive in unless this is something that the community would want and would eventually get added back into grails.

The only issues I can think of are possible validation issues on save() if there are other required fields that weren't supplied in the findOrCreateBy... method.

Rails has a similar method, you can find the docs here (search for 'find_or_create_by') : http://api.rubyonrails.org/classes/ActiveRecord/Base.html

Brandon

On Mon, Oct 26, 2009 at 1:10 PM, Graeme Rocher <graeme.rocher@...> wrote:
Can you describe the semantics of the methods with some examples

On Mon, Oct 26, 2009 at 7:33 PM, Brandon Leonardo
<brandon.leonardo@...> wrote:
> Hi Everyone,  I think creating a dynamic findOrCreateBy or
> findOrInitializeBy method would be a nice addition to GORM.  Was wondering
> if you guys had thoughts on it? If you like I'll do the work and generate a
> pull request.
> Brandon



--
Graeme Rocher
Head of Grails Development
SpringSource - Weapons for the War on Java Complexity
http://www.springsource.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email




Re: findOrCreateBy or findOrInitializeBy

by Brandon-64 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ah, just saw this reply. You put it quite a bit more succinctly than I did.  I've heard of but never looked into GORM Labs but now that I see it I like what you're doing with it.  Maybe it should go into there first?

On Mon, Oct 26, 2009 at 6:08 PM, Robert Fischer <robert.fischer@...> wrote:
I've got this on my todo list for GORM Labs.  Had the start of a prototype, but it never got quite finished.

Foo.findOrCreateByBar(bar)
meant
Foo.findByBar(bar) ?: new Foo(bar:bar).save()


I also had a more generic but analogous "findOrCreateWhere" (which actually provided the implementation for all the findOrCreateByXXX methods).

~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Grails Expert Retainer Services
http://smokejumperit.com/grails-retainer/



Brandon Leonardo wrote:
Hi Everyone,  I think creating a dynamic findOrCreateBy or findOrInitializeBy method would be a nice addition to GORM.  Was wondering if you guys had thoughts on it? If you like I'll do the work and generate a pull request.

Brandon

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email




Re: findOrCreateBy or findOrInitializeBy

by Robert Fischer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

If it goes into GORM Labs, it will be available immediately for projects: if it goes into Grails,
you will have to wait for a release (and possibly a major release).  This is part of the reason GORM
Labs exists.  My suggestion would be to start with GORM Labs to prove out the functionality and
shake out some of the API, and then open a JIRA ticket with Grails to get it moved into the core.

~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Grails Expert Retainer Services
http://smokejumperit.com/grails-retainer/


Brandon Leonardo wrote:

> Ah, just saw this reply. You put it quite a bit more succinctly than I
> did.  I've heard of but never looked into GORM Labs but now that I see
> it I like what you're doing with it.  Maybe it should go into there first?
>
> On Mon, Oct 26, 2009 at 6:08 PM, Robert Fischer
> <robert.fischer@...
> <mailto:robert.fischer@...>> wrote:
>
>     I've got this on my todo list for GORM Labs.  Had the start of a
>     prototype, but it never got quite finished.
>
>     Foo.findOrCreateByBar(bar)
>     meant
>     Foo.findByBar(bar) ?: new Foo(bar:bar).save()
>
>
>     I also had a more generic but analogous "findOrCreateWhere" (which
>     actually provided the implementation for all the findOrCreateByXXX
>     methods).
>
>     ~~ Robert Fischer, Smokejumper IT Consulting.
>     Enfranchised Mind Blog http://EnfranchisedMind.com/blog
>
>     Grails Expert Retainer Services
>     http://smokejumperit.com/grails-retainer/
>
>
>
>     Brandon Leonardo wrote:
>
>         Hi Everyone,  I think creating a dynamic findOrCreateBy or
>         findOrInitializeBy method would be a nice addition to GORM.  Was
>         wondering if you guys had thoughts on it? If you like I'll do
>         the work and generate a pull request.
>
>         Brandon
>
>
>     ---------------------------------------------------------------------
>     To unsubscribe from this list, please visit:
>
>       http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email