views:

94

answers:

1

Hi, I'm calling a rails app from another using ActiveResource. I need to supply the id of the new object to the first app (yes, controller create in this app knows how to handle receiving an id), so I do something like this:

a = ActiveResourceModel.new(:id => 1231231, :name => "test")
a.save

However, instead of doing POST to create a new resource it PUTs it, causing the receiving app to try to update the resource with id 1231231, which of course doesn't exist (I want to create it!), so I end up receiving a 404 error because of this.

Doing some testing the problem seems to be in ActiveResourceModel.new? which returns false, while ActiveResourceModel.exists? returns false too (Great, two methods which are supposed to be opposite return the same!).

+1  A: 

Checking the AResource source and documentation, the new? method checks for the presence of the id and the exists? checks for the remote resource, making both returning the same.

Why exactly you need to pass the id to create a new object? Doesn't make sense. Anyway, You can try to call create method instead of save.

Lucas
I forgot to add, create is the same as new+save.The id thing... The entities i will be creating are the same in both apps and need to be synchronized. Instead of having a mapping between the ids in each application we decided it would be easier to use uuids as ids and pass those around.
diegogs
create tries to post the resource directly. http://github.com/rails/rails/blob/master/activeresource/lib/active_resource/base.rb#L1327You should avoid replication like that imho. a 3rd app to center the common data may be less problematic.
Lucas