Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[PATCH] Make has_one with :conditions hash scope build or creation of object with those conditions

#3088

When building or creating a has_many association, the conditions specified as a hash in the association are merged into the attributes of the new object. As explained in the docs:

Record creations from the association are scoped if a hash is used. has_many :posts, :conditions => {:published => true} will create published posts with @blog.posts.create or @blog.posts.build.

This patch reflects the same behavior for has_one. If accepted, this can also be applied also to belongs_to, or maybe for [build|create]_association in Reflection. The result of this patch is also explained in the doc of has_one:

Record creation from the association is scoped if a hash is used. has_one :account, :conditions => {:enabled => true} will create an enabled account with @company.create_account or @company.build_account.

Reported by Luciano G. Panaro · August 22nd, 2009 @ 07:24 PM

State: committed
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from 2-3-stable, :conditions, has_one to 2-3-stable, :conditions, bugmash, has_one

    September 25th, 2009 @ 12:47 PM

  2. John Pignata
    John Pignata

    +1 on the feature - this makes a lot of sense to also be a part of has_one associations.

    -1 on the patch as I couldn't get it to cleanly apply to 2-3-stable. As this is a new feature and not a maintenance fix it should be written as a patch to master though anyway.

    September 26th, 2009 @ 03:11 AM

  3. Anuj Dutta
    Anuj Dutta
    • Tag changed from 2-3-stable, :conditions, bugmash, has_one to 2-3-stable, :conditions, bugmash, bugmash-patch, has_one

    +1 on the feature - makes sense

    September 26th, 2009 @ 01:17 PM

  4. Anuj Dutta
    Anuj Dutta
    • Tag changed from 2-3-stable, :conditions, bugmash, bugmash-patch, has_one to 2-3-stable, :conditions, bugmash-patch, has_one

    September 26th, 2009 @ 01:18 PM

  5. Anuj Dutta
    Anuj Dutta
    • Tag changed from 2-3-stable, :conditions, bugmash-patch, has_one to 2-3-stable, :conditions, bugmash, bugmash-patch, has_one

    September 26th, 2009 @ 01:20 PM

  6. Luciano G. Panaro
    Luciano G. Panaro

    Taking a look at the patch - will fix it right away

    September 26th, 2009 @ 02:11 PM

  7. Luciano G. Panaro
  8. Elomar França
    Elomar França

    +1 for this feature.

    Both patches from luciano.panaro applied successfully.

    September 26th, 2009 @ 07:32 PM

  9. Anuj Dutta
    Anuj Dutta

    Patch applies successfully on 2-3-stable. Green on tests.

    September 27th, 2009 @ 12:01 AM

  10. sr.iniv.t
    sr.iniv.t

    +1 verified.

    The patches apply cleanly and all tests pass. I've always wanted this feature on AR.

    September 27th, 2009 @ 04:37 AM

  11. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from 2-3-stable, :conditions, bugmash, bugmash-patch, has_one to 2-3-stable, :conditions, bugmash, bugmash-review, has_one

    September 27th, 2009 @ 11:55 AM

  12. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from 2-3-stable, :conditions, bugmash, bugmash-review, has_one to 2-3-stable, :conditions, bugmash-review, has_one

    September 27th, 2009 @ 12:34 PM

  13. Repository
    Repository
    • State changed from new to committed

    (from [14a6794a8ef9995c45a86e95c45d315c61087103]) Make has_one with :conditions hash scope build or creation of the associated object with those conditions

    Signed-off-by: Michael Koziarski michael@koziarski.com
    [#3088 [PATCH] Make has_one with :conditions hash scope build or creation of object ... state:committed] http://github.com/rails/rails/commit/14a6794a8ef9995c45a86e95c45d31...

    September 28th, 2009 @ 03:29 AM

  14. Repository
  15. Rizwan Reza
    Rizwan Reza
    • Tag changed from 2-3-stable, :conditions, bugmash-review, has_one to 2-3-stable, :conditions, has_one

    May 15th, 2010 @ 06:42 PM

  16. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 11th, 2010 @ 10:55 AM