# Orphans

**URL:** <https://www.ruby-forum.com/t/orphans/127058>\
**Category:** Rails\
**Created:** [January 24, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/orphans/127058 "2008-01-24T17:17:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [January 24, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/orphans/127058/1 "2008-01-24T17:17:59Z")

</div>

I’m trying to solve the problem of orphaned records, and wondering what  
the best solution is, maybe someone can help me with a little example  
code.

I have the following models.

Enquiry  
Course

and when someone generates an enquiry they select a course, it all works  
so that I can do the following.

Enquiry.course.price\_per\_week \* Enquiry.course\_duration

Works great.

However I have a problem I need to solve.

The administrator should be able to add, update and delete enquiries and  
also add, update and delete courses. I know this can cause “orphaning”  
problems as if an enquiry exists but the course for that enquiry has  
been destroyed the record will be invalid and my application will crash.

I know about several ways to solve this but I’d like opinions on which  
is best and how to achieve it.

For the time being I removed the ability of the admin to delete courses  
(was thinking of putting in an “archive” boolean to just switch it off),  
but then I’ll end up with lots of old rubbish in the DB.

Hope this makes sense? bb.

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [January 24, 2008, 5:29pm UTC](https://www.ruby-forum.com/t/orphans/127058/2 "2008-01-24T17:29:47Z")

</div>

On 24 Jan 2008, at 16:17, bingo bob wrote:

> The administrator should be able to add, update and delete enquiries  
> courses  
> (was thinking of putting in an “archive” boolean to just switch it  
> off),  
> but then I’ll end up with lots of old rubbish in the DB.

I’ve often gone down the archive route, but that sort of depends on  
whether you see it as old rubbish or a valuable audit trail

Fred

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [January 24, 2008, 6:18pm UTC](https://www.ruby-forum.com/t/orphans/127058/3 "2008-01-24T18:18:39Z")

</div>

> I’ve often gone down the archive route, but that sort of depends on  
> whether you see it as old rubbish or a valuable audit trail
> 
> Fred

OK, haha. sold. Archival and audit trail it is.

One thing that always confuses me …

For my select drop down (the drop down in the enquiry form that selects  
all the courses), I’ll need to make it only select the courses that are  
not archived.

I should do this in the model, right?

eg find\_all\_active\_courses … some code to find courses where they’re  
not archived.

any example of the model code and the view code would be v much  
apprecaited.

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [January 24, 2008, 10:03pm UTC](https://www.ruby-forum.com/t/orphans/127058/4 "2008-01-24T22:03:51Z")

</div>

class Course \< ActiveRecord::Base  
has\_many :enquiries, :dependent =\> true

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [January 24, 2008, 9:37pm UTC](https://www.ruby-forum.com/t/orphans/127058/5 "2008-01-24T21:37:39Z")

</div>

On 24 Jan 2008, at 17:18, bingo bob wrote:

> One thing that always confuses me …  
> not archived.  
> definitely! You will probably have to swap a fair few instances of  
> Courses.find :all with Courses.find\_all\_active

You can do something like

def self.find\_active(\*args)  
with\_scope(:find =\> {:conditions =\> {:active =\> true}}) do  
find \*args  
end  
end

Like that your find\_active method can take all the options that a  
regular find would, except it will never return inactive courses.

Also check out your associations. it may be appropriate to add  
conditions to those.

Fred

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [January 25, 2008, 2:40am UTC](https://www.ruby-forum.com/t/orphans/127058/6 "2008-01-25T02:40:12Z")

</div>

Keynan, do you mean :dependent =\> :destroy? I’m not to sure on the true  
option, but you’re probably right.

On Jan 25, 2008 7:33 AM, Keynan P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## class Course \< ActiveRecord::Base has\_many :enquiries, :dependent =\> true
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [January 25, 2008, 10:32am UTC](https://www.ruby-forum.com/t/orphans/127058/7 "2008-01-25T10:32:30Z")

</div>

from what I can work out fred’s on the right lines for me.

I know about dependent destroy (have used it elsewhere, but in my case I  
don;t think it’ll help me.

I mean they way i see it destroying an enquiry is fine (as maybe a  
spammer has generated a lot of bogus ones or it’s just junk)…but a  
course must not be deleted as it’ll break the App (the selection box on  
my enquiry form finds all courses).

ta

bb
