0

We are developing a Rails app that integrates with an existing application. We are using Mule and restful interfaces to move data between the 2 applications.

We have a few code tables like Priority and Types that come from the old app to the new app. If we can use the same ID numbers for both applications, it will be easier to move data records that use those IDs for foreign-keys.

Is there a way to allow the ID to be one of the fields that gets updated via the Rails restful interface?

I would like this to work:

http://localhost:5000/types?type[id]=315&type[typecode]=test

This is the type controller for POST:

  # POST /types
# POST /types.json
def create
  @type = Type.new(params[:type])

  respond_to do |format|
    if @type.save
      format.html { redirect_to @type, notice: 'Type was successfully created.' }
      format.json { render json: @type, status: :created, location: @type }
      format.xml { render xml: @type, status: :created, location: @type }
    else
      format.html { render action: "new" }
      format.json { render json: @type.errors, status: :unprocessable_entity }
      format.xml { render xml: @type.errors, status: :unprocessable_entity }
    end
  end
end

Right now, if I post that URL, it creates a new type record with typecode=test, but the id is the next available id instead of the one I gave it in the URL.

Thanks for the help!

Reddirt
  • 5,913
  • 9
  • 48
  • 126
  • Please provide more of your source code and anything you've tried already. – Tomanow Apr 19 '13 at 17:52
  • 1
    Why would you want to do this? The point of the ID is to be guaranteed unique and be a set link to the record. – MrDanA Apr 19 '13 at 17:53
  • possible duplicate of [Overriding id on create in ActiveRecord](http://stackoverflow.com/questions/431617/overriding-id-on-create-in-activerecord) – Andrew Apr 19 '13 at 18:57
  • ID is a protected attribute and cannot be set with mass assignment. That's your issue. See: http://stackoverflow.com/questions/431617/overriding-id-on-create-in-activerecord – Andrew Apr 19 '13 at 18:58

1 Answers1

0

Did you try this?

      @type = Type.new(params[:type])
      if params[:type][:id]
          @type.id = params[:type][:id]
      end
      @type.save
shishirmk
  • 425
  • 2
  • 14