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.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

association_collection#create acts as association_collection#create!

#2934

ActiveRecord has_many :through's association_collection#create seems to act as association_collection#create!, raising errors instead of merely building the AR object instance with the appropriate errors.

  #example
  http://github.com/stefanpenner/association_collection_create_bug/

Example

  #results in an error being raised.
  group.users.create(..with..invalid..user..attributes..)

Specs

  #Failing specs are included.
  rake

Workaround

  #Hackish Workaround.
  @user = group.users.create(..with..invalid..user..attributes..) rescue     User.build(..with..invalid..user..attributes..)

Reported by stefanpenner · July 21st, 2009 @ 09:13 PM

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

Activity

  1. Michael Koziarski
    Michael Koziarski
    • Tag changed from activerecord to activerecord, bugmash

    August 3rd, 2009 @ 06:17 AM

  2. railsbob
    railsbob

    Verified

    I was able to reproduce this error with the help of mentioned example.

    August 8th, 2009 @ 12:21 PM

  3. railsbob
    railsbob

    I have attached a patch for the failing test case.

    August 8th, 2009 @ 06:01 PM

  4. railsbob
  5. Dan Croak
    Dan Croak

    -1 patches do not apply cleanly in 2-3-stable

    August 8th, 2009 @ 08:09 PM

  6. José Valim
    José Valim

    Just to add a notice, I asked @railsbob to rebase the patch and improve test coverage. Here is a example of tests for has_many: http://pastie.org/576732

    August 8th, 2009 @ 08:16 PM

  7. railsbob
    railsbob

    Sorry, please ignore the previous patch (whitespace error). This one applies cleanly.

    August 8th, 2009 @ 09:04 PM

  8. railsbob
    railsbob

    As discussed with lifo, the previous patch is modifying << method. I am attaching another patch, which removes << from create method.

    August 8th, 2009 @ 11:48 PM

  9. Rizwan Reza
    Rizwan Reza

    verified

    +1 The patch works cleanly on master but does not in 2-3-stable.

    August 8th, 2009 @ 11:54 PM

  10. Dan Croak
    Dan Croak

    verified 2934_fixed.diff does not apply cleanly to 2-3-stable.

    August 10th, 2009 @ 02:18 AM

  11. Dan Croak
    Dan Croak

    I've attached a patch that works for 2-3-stable. It was modified from railsbob's 2934_fixed.diff patch for master. I don't think it is possible to have a single patch that will work for both, at least without serious refactoring of one of the tests.

    August 10th, 2009 @ 02:33 AM

  12. Jeremy Kemper
    Jeremy Kemper
    • State changed from new to committed
    • Tag changed from activerecord, bugmash to activerecord
    • Milestone cleared.

    August 10th, 2009 @ 02:52 AM

  13. Repository
  14. Repository