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.

ActiveResource should work for non-generated ids

#5660

ActiveResource should allow you to define a primary key which doesn't get generated on the server.

example:

module Recurly
  class Plan < ActiveResouce::Base
    self.element_name = "plan"
    self.prefix = "/company/"
    self.primary_key = :plan_code
  end
end


new_plan = Recurly::Plan.new(:plan_code => "my_new_plan")
new_plan.save! # performs an update instead of a create

The issue is ActiveResource::Base#new? just looks at whether an id exists or not to decide whether or not to update or create. I think it needs to be a bit smarter and track this flag based on whether the record was actually saved onto the server (or found via a query). If the flag is not set, then it should create the record.

Reported by Jacques Crocker · September 19th, 2010 @ 02:57 AM

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

Activity

  1. Jacques Crocker
    Jacques Crocker
    • Tag changed from activeresource, active_resource, resource to activeresource, active_resource, patched, resource

    Was a fairly simple fix.

    Patched via the topic branch:
    http://github.com/railsjedi/rails/commits/nongeneratedids

    Pull request sent. Please review and merge if possible

    September 19th, 2010 @ 04:22 AM

  2. gnufied
    gnufied

    Couple of things:

    1. Your pull request, doesn't merge cleanly agains master branch.
    2. You have added:
      @@@ ruby require 'fixtures/address' @@@ But the file isn't in your patch.
    3. Probably you are better off, creating a patch using format-patch rather than sending a pull request.

    September 22nd, 2010 @ 05:20 PM

  3. Jacques Crocker
    Jacques Crocker

    gnufied: thanks for the heads up. i was trying to juggle 2 patches at the same time and got confused in splitting up the branches. has been fixed now: http://github.com/railsjedi/rails/tree/nongeneratedids

    I do think a topic branch is the right approach as I can make fixes like this easily without having to regenerating a patch.

    Please retest if you can. I just verified cherry picking it into origin/master and everything ran silky smooth :-)

    September 22nd, 2010 @ 10:38 PM

  4. Repository
    Repository
    • State changed from new to resolved

    (from [ee5ef67c443407f616feef3a8cade8ba3a9d6ef0]) Allow ActiveResource to work with non-generated ids [#5660 ActiveResource should work for non-generated ids state:resolved]

    This commit updates new? so that it knows whether or not the record was actually new or not, and doesn't rely solely on the presence of id. This enables the ability to set a custom primary_key that is not autogenerated by the server.

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/ee5ef67c443407f616feef3a8cade8...

    September 25th, 2010 @ 10:16 AM

  5. Andrea Campi
    Andrea Campi
    • Tag changed from activeresource, active_resource, patched, resource to activeresource, active_resource, patch, resource

    bulk tags cleanup

    October 11th, 2010 @ 07:23 AM

  6. Raffael Schmid
    Raffael Schmid

    could it be, that this wasn't merged into main?

    February 21st, 2011 @ 04:22 PM