# How to create a permanent record in table

**URL:** <https://www.ruby-forum.com/t/how-to-create-a-permanent-record-in-table/158361>\
**Category:** Rails\
**Created:** [January 22, 2009, 1:28pm UTC](https://www.ruby-forum.com/t/how-to-create-a-permanent-record-in-table/158361 "2009-01-22T13:28:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [January 22, 2009, 1:28pm UTC](https://www.ruby-forum.com/t/how-to-create-a-permanent-record-in-table/158361/1 "2009-01-22T13:28:48Z")

</div>

I want one record in my table to be undeletable by a user. I guess/know  
i can achieve this by writing code in my controller and model to only  
present the user with reocords which can be deleted but im wondering if  
there are any magic rails methods that can do this in a concrete way?

background: Im writing a todolist which has two tables, tasks and  
projects whih have a standard has\_many relationship. ie. a project has  
many tasks.

When a user creates a task they specify whihc project they want to add  
it to.  
If a task is just a one off and not aprt of any project the user can  
specify “No project” in teh project dropdown. this in fact is a project  
but just using it to create the illusion! this is the record i dont want  
the user to be able to delete.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [January 22, 2009, 2:01pm UTC](https://www.ruby-forum.com/t/how-to-create-a-permanent-record-in-table/158361/2 "2009-01-22T14:01:52Z")

</div>

On Thu, Jan 22, 2009 at 2:28 PM, Adam A. \<  
removed\_email\_address@domain.invalid\> wrote:

> ## When a user creates a task they specify whihc project they want to add it to. If a task is just a one off and not aprt of any project the user can specify “No project” in teh project dropdown. this in fact is a project but just using it to create the illusion! this is the record i dont want the user to be able to delete.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

Set the model readonly

class MyModel \< ActiveRecord::Base  
def initialize(\*args)  
super(\*args)  
readonly!  
end  
end

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [January 27, 2009, 4:39pm UTC](https://www.ruby-forum.com/t/how-to-create-a-permanent-record-in-table/158361/3 "2009-01-27T16:39:42Z")

</div>

Andrew T. wrote:

> Set the model readonly
> 
> class MyModel \< ActiveRecord::Base  
> def initialize(\*args)  
> super(\*args)  
> readonly!  
> end  
> end  
> Wouldn’t this make all records of the table read only? I don’t think  
> that’s what the OP wanted.

I’d say use the the before\_destroy callback to validate the deletion:

def before\_destroy  
!project\_name.eql?(“No project”)  
end

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [January 27, 2009, 5:28pm UTC](https://www.ruby-forum.com/t/how-to-create-a-permanent-record-in-table/158361/4 "2009-01-27T17:28:57Z")

</div>

Robert W. wrote:

> I’d say use the the before\_destroy callback to validate the deletion:
> 
> def before\_destroy  
> !project\_name.eql?(“No project”)  
> end

On second thought this might be a better approach:

class Project \< ActiveRecord::Base  
before\_destroy :protect\_no\_project

private  
def protect\_no\_project  
!name.eql?(“No project”)  
end  
end

It sounds like overriding the callback directly may not be the best  
approach.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [January 28, 2009, 10:31am UTC](https://www.ruby-forum.com/t/how-to-create-a-permanent-record-in-table/158361/5 "2009-01-28T10:31:57Z")

</div>

Thanks everyone for your input. Yes I did only want to protect one of  
the records so ill give your (roberts) method a shot!

thank you very much
