# About before\_\* callbacks

**URL:** <https://www.ruby-forum.com/t/about-before-callbacks/162038>\
**Category:** Rails\
**Created:** [March 5, 2009, 5:14am UTC](https://www.ruby-forum.com/t/about-before-callbacks/162038 "2009-03-05T05:14:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cfc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9a140/32.png) [@cfc](https://www.ruby-forum.com/u/cfc)\
**Post date:** [March 5, 2009, 5:14am UTC](https://www.ruby-forum.com/t/about-before-callbacks/162038/1 "2009-03-05T05:14:08Z")

</div>

Hi  
I have an problem about before\_\* callbacks.

I have a model named: Event  
And I don’t want anyone to destroy or update any records in Event  
model.  
So:

```auto
class ActiveRecord::Base
  before_destroy :log_illegal_operations
  before_update :log_illegal_operations

  private

  def log_illegal_operations
    Event.create({:key => "illegal operation", :value => "Someone
wants to update/destroy record(s) in Event model. Blocked!"})
    raise ActiveRecord::ReadOnlyRecord, "This is a readonly model, do
not destroy any record!"
    # return false
  end
end
```

But, it won’t create any events but the auto\_increment will be  
increment.  
Do anybody knows how to solve it?

Thanks!

---

<div class="post-metadata">

**Author:** ![cfc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9a140/32.png) [@cfc](https://www.ruby-forum.com/u/cfc)\
**Post date:** [March 5, 2009, 6:12am UTC](https://www.ruby-forum.com/t/about-before-callbacks/162038/2 "2009-03-05T06:12:49Z")

</div>

Billy H. wrote:

> I have a model named: Event  
> And I don’t want anyone to destroy or update any records in Event  
> model.  
> So:

How about instead making the model read-only:

> **[ActiveRecord Read Only Model · devroom.io](https://www.devroom.io/2008/08/17/activerecord-read-only-models/)**
>
> Ariejan de Vroom's personal website on software engineering, electronics tinkering and car mechanics.

---

<div class="post-metadata">

**Author:** ![cfc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9a140/32.png) [@cfc](https://www.ruby-forum.com/u/cfc)\
**Post date:** [March 5, 2009, 7:12am UTC](https://www.ruby-forum.com/t/about-before-callbacks/162038/3 "2009-03-05T07:12:13Z")

</div>

If I set read-only to the model, then I can’t create any event  
anymore ☹  
I just want to block anyone to update/destroy records, but I need to  
create records 🙂

On 3e$B7ne(B5e$BF|e(B, e$B2\<8ae(B1e$B;~e(B12e$BJ,e(B, Robert W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cfc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9a140/32.png) [@cfc](https://www.ruby-forum.com/u/cfc)\
**Post date:** [March 5, 2009, 10:36am UTC](https://www.ruby-forum.com/t/about-before-callbacks/162038/4 "2009-03-05T10:36:47Z")

</div>

On Mar 5, 6:11 am, CFC [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If I set read-only to the model, then I can’t create any event  
> anymore ☹  
> I just want to block anyone to update/destroy records, but I need to  
> create records 🙂

Check whether new\_record? is true before returning false from your  
callback ?

Fred

---

<div class="post-metadata">

**Author:** ![cfc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9a140/32.png) [@cfc](https://www.ruby-forum.com/u/cfc)\
**Post date:** [March 5, 2009, 5:22pm UTC](https://www.ruby-forum.com/t/about-before-callbacks/162038/5 "2009-03-05T17:22:39Z")

</div>

I think the only way to do this is write the code in destroy & update  
action  
☹  
If one callback return false, then it will interrupt any actions after  
the  
callback. ☹

Thank you, Fred and Robert 😃

On Thu, Mar 5, 2009 at 5:35 PM, Frederick C.  
\<removed\_email\_address@domain.invalid

> wrote:

> callback ?
> 
> > > How about instead making the model read-only:
> > 
> > > ## [ActiveRecord Read Only Model · devroom.io](http://ariejan.net/2008/08/17/activerecord-read-only-models/)
> > > 
> > > Posted viahttp://www.ruby-forum.com/.

–  
Only two surfaces of a box:  
[http://blog.pixnet.net/zusocfc](http://blog.pixnet.net/zusocfc)

---

<div class="post-metadata">

**Author:** ![cfc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9a140/32.png) [@cfc](https://www.ruby-forum.com/u/cfc)\
**Post date:** [March 8, 2009, 5:39am UTC](https://www.ruby-forum.com/t/about-before-callbacks/162038/6 "2009-03-08T05:39:32Z")

</div>

Okay, I seeThanks, Matt.

On Fri, Mar 6, 2009 at 12:31 AM, Matt J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [snip]  
> Is there some reason that this can’t be done in the UI? Just don’t  
> provide the update and delete options…
> 
> >

–  
Only two surfaces of a box:  
[http://blog.pixnet.net/zusocfc](http://blog.pixnet.net/zusocfc)

---

<div class="post-metadata">

**Author:** ![cfc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9a140/32.png) [@cfc](https://www.ruby-forum.com/u/cfc)\
**Post date:** [March 5, 2009, 5:32pm UTC](https://www.ruby-forum.com/t/about-before-callbacks/162038/7 "2009-03-05T17:32:39Z")

</div>

On Mar 4, 11:13 pm, CFC [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi  
> I have an problem about before\_\* callbacks.
> 
> I have a model named: Event  
> And I don’t want anyone to destroy or update any records in Event  
> model.  
> [snip]  
> But, it won’t create any events but the auto\_increment will be  
> increment.  
> Do anybody knows how to solve it?
> 
> Thanks!

The problem you’ve run into is that the callbacks run inside a  
transaction - throwing the ReadOnlyRecord exception rolls the whole  
thing back (including the Event record).

Is there some reason that this can’t be done in the UI? Just don’t  
provide the update and delete options…
