Hi,
I have two models, Page and MenuItem, where Page belongs_to MenuItem .
When I delete a Page I need to delete also its MenuItem, and vice versa,
but the problem is that I can’t put a :dependent => :destroy on both
relations because (obviously) this gives an error.
Is there a clean way to solve this?
Thanks!
On 2 April 2012 17:25, Serafino P. [email protected] wrote:
Hi,
I have two models, Page and MenuItem, where Page belongs_to MenuItem .
When I delete a page I need to delete also its MenuItem, and vice versa.
The problema is that I can’t put a :dependent => :destroy on both
relations because (obviously) this gives an error.
Is there a clean way to solve this?
You have not told us what is the reverse association between MenuItem
and Page.
Colin
There is no reverse, it’s a regular has_one belongs_to association. i
just wanto to be able to delete both records either by deleting a page
from the pages_controller or by deleting a menu_item from the
menu_items_controller. in both cases both the page and the menu item
need to be deleted like a dependent association, but it’s impossible to
put dependent => destroy on both
On Mon, Apr 2, 2012 at 3:10 PM, Serafino P. [email protected]
wrote:
There is no reverse, it’s a regular has_one belongs_to association. i
just wanto to be able to delete both records either by deleting a page
from the pages_controller or by deleting a menu_item from the
menu_items_controller. in both cases both the page and the menu item
need to be deleted like a dependent association, but it’s impossible to
put dependent => destroy on both
What makes you think that?
–
Hassan S. ------------------------ [email protected]
twitter: @hassan
On Mon, Apr 2, 2012 at 3:48 PM, Serafino P. [email protected]
wrote:
There is no reverse, it’s a regular has_one belongs_to association. i
just wanto to be able to delete both records either by deleting a page
from the pages_controller or by deleting a menu_item from the
menu_items_controller. in both cases both the page and the menu item
need to be deleted like a dependent association, but it’s impossible to
put dependent => destroy on both
What makes you think that?
I tried. With dependent destroy on both the belongs_to and has_one when
i delete one i get an error about not finding the other record
Then you have a different problem, because it works for me 
e.g.
class Ship < ActiveRecord::Base
has_one :captain, :dependent => :destroy
end
class Captain < ActiveRecord::Base
belongs_to :ship, :dependent => :destroy
end
Calling ‘destroy’ on either gets rid of both, as expected.
–
Hassan S. ------------------------ [email protected]
twitter: @hassan
Hassan S. wrote in post #1054715:
On Mon, Apr 2, 2012 at 3:10 PM, Serafino P. [email protected]
wrote:
There is no reverse, it’s a regular has_one belongs_to association. i
just wanto to be able to delete both records either by deleting a page
from the pages_controller or by deleting a menu_item from the
menu_items_controller. in both cases both the page and the menu item
need to be deleted like a dependent association, but it’s impossible to
put dependent => destroy on both
What makes you think that?
–
Hassan S. ------------------------ [email protected]
Hassan Schroeder | about.me
twitter: @hassan
I tried. With dependent destroy on both the belongs_to and has_one when
i delete one i get an error about not finding the other record
Probably then it’s related to the awesome_nested_set plugin… This
sucks it’s already the second plugin I’m trying to use (the other is
cancan) that breaks some core functionality
On Mon, Apr 2, 2012 at 4:19 PM, Serafino P. [email protected]
wrote:
Probably then it’s related to the awesome_nested_set plugin…
Well, I just added awesome_nested_set (as a gem) to my little test
app, and it didn’t have any effect on using :dependent => :destroy.
You might want to build your own separate test app and add things
in one by one to isolate the problem.
HTH,
Hassan S. ------------------------ [email protected]
twitter: @hassan
On Apr 2, 2012, at 9:06 PM, Hassan S. wrote:
On Mon, Apr 2, 2012 at 4:19 PM, Serafino P. [email protected] wrote:
Probably then it’s related to the awesome_nested_set plugin…
Well, I just added awesome_nested_set (as a gem) to my little test
app, and it didn’t have any effect on using :dependent => :destroy.
You might want to build your own separate test app and add things
in one by one to isolate the problem.
Ditto for CanCan – no issues here with nested records and delete
options.
Walter
On 2 April 2012 23:10, Serafino P. [email protected] wrote:
There is no reverse, it’s a regular has_one belongs_to association.
The reverse relation is then MenuItem has_one Page. That is what you
had not told us.
Colin
First of all thank you, I didn’t expect you to go that far testing. The
point however is that the problem is solved if i remove
awesome_nested_set from the model, and I don’t have any other plugin or
extension applied to the model/project except from devise, but it’s only
related to the user model.