Strictly related models

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?

Thanks!

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

What makes you think that?

Hassan Schroeder wrote in post #1054715:

Then you have a different problem, because it works for me :slight_smile: 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.

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

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,

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

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

The reverse relation is then MenuItem has_one Page. That is what you had not told us.

Colin