RESTful deleting a resource without a resource id


#1

In a situation where an Order has many Orderlines and an Item has many
Orderlines, I want to call DELETE /orderlines/?order_id=1&item_id=1
however the REST restriction don’t allow this by default, right?

When adding ‘map.resources :orderlines’ to routes.rb, the DELETE route
on the OrderlinesController expects params[:id] to contain the
Orderline to delete, I could put my order_id in this parameter, but
that’s a bit messy.

Would it be possible to disable the :id requirement (and maybe add one
for item_id and order_id) in the routes definition?


#2

In a situation where an Order has many Orderlines and an Item has many
Orderlines, I want to call DELETE /orderlines/?order_id=1&item_id=1
however the REST restriction don’t allow this by default, right?

Sounds like you’re trying to use compound primary keys. I would think
it easier if you just gave each orderline a single id. Could even just
be a combination of the two, so DELETE /orderlines/1-1.


#3

Let me try to answer…

2007/4/27, Matthijs L. removed_email_address@domain.invalid:

In a situation where an Order has many Orderlines and an Item has many
Orderlines, I want to call DELETE /orderlines/?order_id=1&item_id=1
however the REST restriction don’t allow this by default, right?

I’m not sure I understand you here maybe you are saying that an Order
has_many items and an item has_many orderlines or are you saying that
an
order has many items thorugh orderlines?

The latter seems more reasonable, thus:
since it seems that you are trying to modify an order by removing its
orderlines why don’t you

DELETE /orders/1/items/1

instead? You could do this with:

map.resources :orders do |order|
order.map :items
end

Could it work?

bye
Luca


#4

OrderlinesController#destroy(order_id, item_id), but as Luca pointed
out, the ItemsController may actually be a better place, what do you
think?

I don’t think the ItemsController is a good fit for this. But you
could direct /orders/5/items/5 at an OrderlinesController. But that’s
still weird. What if I have 3 of the same item in my order?

I dislike compound keys always. But if you HAVE to, just invent your
own scheme. Like order_id, item_id = params[:id].split("-"). And
request that with something like /orderlines/5-23. But I think that’s
a smell.


#5

2007/5/3, DHH removed_email_address@domain.invalid:

I don’t think the ItemsController is a good fit for this. But you
could direct /orders/5/items/5 at an OrderlinesController. But that’s
still weird.

Agreed, I’d like much more /orders/5/orderlines/5 because orderlines of
an
order are what you are deleting, but Matthijs description sounds like an
“has and belongs to many” relation turned into an “has many through”
where
orders and items are linked through orderlines but he is considering the
orderlines mostly as a relation
, not a model on their own. (hence order_id+item_id identifies the
correlated objects)

What if I have 3 of the same item in my order?

I guess it would depend on the cardinality of the relation…

I dislike compound keys always. But if you HAVE to, just invent your

own scheme. Like order_id, item_id = params[:id].split("-"). And
request that with something like /orderlines/5-23. But I think that’s
a smell.

bleah, me too :slight_smile:

Luca


#6

Thanks for your reply David and Luca,

In respond to Luca:
When I’m calling DELETE /orders/1/items/1 I’m ending up in
ItemsController#destroy, right? There I find params[:order_id]
containing the id of the order and params[:id] the id of the item.
Now is this ItemController action allowed to invoke
Orderlines.find(:first, :conditions => { :order_id =>
params[:order_id], :item_id => params[:id] }).destroy? Or is this the
job of the destroy action of the OrderlinesController?

In respond to DHH:
An Orderline has an unique id, however in some views I do know the
Order I’m dealing with and the item I want to remove from this order,
but I don’t know the id of the Orderline which is connecting the Order
and the Item, that’s why I want to call
OrderlinesController#destroy(order_id, item_id), but as Luca pointed
out, the ItemsController may actually be a better place, what do you
think?

It’s now clear to me that this odd way of deleting Orderlines started
when I was iterating through Order#items where an Order has_many items
through orderlines. Then I don’t have the orderline id available
directly, however some requirements changed recently forcing me to
iterate through Order#orderlines, so now I do have the orderline_id
available to call OrderlinesController#destroy(id), however I’m still
curious about answers to the questions asked above.


#7

On 4/27/07, DHH removed_email_address@domain.invalid wrote:

In a situation where an Order has many Orderlines and an Item has many
Orderlines, I want to call DELETE /orderlines/?order_id=1&item_id=1
however the REST restriction don’t allow this by default, right?

Sounds like you’re trying to use compound primary keys. I would think
it easier if you just gave each orderline a single id. Could even just
be a combination of the two, so DELETE /orderlines/1-1.

Use a singular resource?

DELETE orders/1/items/1/orderline


Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com