Orphans

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.

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

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.

class Course < ActiveRecord::Base
has_many :enquiries, :dependent => true

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

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. [email protected]
wrote:

class Course < ActiveRecord::Base
has_many :enquiries, :dependent => true

Posted via http://www.ruby-forum.com/.


Ryan B.

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

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