views:

839

answers:

3

I have a Bill model with nested Customer model. The Customer model has a phone number with a uniqueness validation on it. While creating the bill I want to fetch the existing record based on the phone number or create a new one if such doesn't exist. How should I do it in a RESTful way?

A: 

You can look at the find_or_create or find_or_create_by methods (which are dynamically created). A little Googling should get you there the rest of the way, I think.

Trevoke
+2  A: 

you would use the find_or_create_by method which would look something like this in your case:

fetchedRecord = Bill.find_or_create_by_phone_number(customer.phone_number)
Patrick Robertson
A: 

It doesn't seem like these answers are what you are asking.

Forget about Rails, my question would be, what's the RESTful way to create a resource that might already exist? Should you POST to the resources (list) URL, and then expect a HTTP status code of 201 if the resource was created and a 200 if it already existed?

Seems like this should be spelled out in a standard somewhere.

By the way, this is how I am handling it--with status codes.

fullware