About before_* callbacks


#1

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:

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!


#2

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:

http://ariejan.net/2008/08/17/activerecord-read-only-models/


#3

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

On 3e$B7ne(B5e$BF|e(B, e$B2<8ae(B1e$B;~e(B12e$BJ,e(B, Robert W.
removed_email_address@domain.invalid


#4

On Mar 5, 6:11 am, CFC removed_email_address@domain.invalid wrote:

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

Check whether new_record? is true before returning false from your
callback ?

Fred


#5

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

Thank you, Fred and Robert :smiley:

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:

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


#6

Okay, I seeThanks, Matt.

On Fri, Mar 6, 2009 at 12:31 AM, Matt J. 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


#7

On Mar 4, 11:13 pm, CFC 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…