Forum: Ruby on Rails RESTful deleting a resource without a resource id.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
49e98c4a13a5203ecff5d2d0774f8991?d=identicon&s=25 Matthijs Langenberg (Guest)
on 2007-04-27 16:08
(Received via mailing list)
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?
6edd67c92a1dab5eb23fed79f3c18564?d=identicon&s=25 DHH (Guest)
on 2007-04-27 16:33
(Received via mailing list)
> 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.
Ddeec660d0ddef3647786f7adb228a97?d=identicon&s=25 Luca Mearelli (Guest)
on 2007-04-27 17:03
(Received via mailing list)
Let me try to answer...

2007/4/27, Matthijs Langenberg <mlangenberg@gmail.com>:
>
>
> 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
49e98c4a13a5203ecff5d2d0774f8991?d=identicon&s=25 Matthijs Langenberg (Guest)
on 2007-05-01 09:55
(Received via mailing list)
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.
6edd67c92a1dab5eb23fed79f3c18564?d=identicon&s=25 DHH (Guest)
on 2007-05-03 00:06
(Received via mailing list)
> 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.
Ddeec660d0ddef3647786f7adb228a97?d=identicon&s=25 Luca Mearelli (Guest)
on 2007-05-03 08:30
(Received via mailing list)
2007/5/3, DHH <david.heinemeier@gmail.com>:

> 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 :)

Luca
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-05-03 10:29
(Received via mailing list)
On 4/27/07, DHH <david.heinemeier@gmail.com> 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 Olson
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com
This topic is locked and can not be replied to.