Deleting ALL info for a user in many tables?

I have this model called “User.” In the user model I have many relations
hips such as:

has_many :creditcards
has_many :airlinememberships

and in my airlinemembership model I have

belongs_to :user

Now, what I would like to do is to delete not only the user but also the
airlinmembership info asssocciated with that user when a delete action
occurs. Now, I suppose I could do a find_by_id( user.id ) to go through
all the tables and destroy all assocciated info, but I’m thinkin that
there has to be an easier way in RoR. Plus, there are many many tables
that a user may have info in which make the code not ugly but certaintly
long.

Also, I know about the rails framework API: http://api.rubyonrails.org/,
but it seems like this site fails to really explain all the attributes
that can be used in a helper method, for example. I was wondering if
anyone knows of a better site that explains maybe a little better or
might provide some better examples? Thanks all,

~S

Shandy N. wrote:

I have this model called “User.” In the user model I have many relations
hips such as:

has_many :creditcards
has_many :airlinememberships

and in my airlinemembership model I have

belongs_to :user

Now, what I would like to do is to delete not only the user but also the
airlinmembership info asssocciated with that user when a delete action
occurs. Now, I suppose I could do a find_by_id( user.id ) to go through
all the tables and destroy all assocciated info, but I’m thinkin that
there has to be an easier way in RoR. Plus, there are many many tables
that a user may have info in which make the code not ugly but certaintly
long.

Also, I know about the rails framework API: http://api.rubyonrails.org/,
but it seems like this site fails to really explain all the attributes
that can be used in a helper method, for example. I was wondering if
anyone knows of a better site that explains maybe a little better or
might provide some better examples? Thanks all,

~S

Couldn’t you just use the foreign key and set the option cascade on
delete? So that way, any record that has your client_id would be
deleted. I hope this helps.

has_many :airlinememberships, :dependent => :destroy

That should do the trick. Look it up for more info.

–Tyler P.

I did the cascade on delete but the :dependent => :destroy is what I was
looking for. Thanks you guys, either way it works perfectly.

~S