About before_* callbacks

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!

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:

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

On Mar 5, 6:11 am, CFC [email protected] 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

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

wrote:

callback ?

How about instead making the model read-only:

ActiveRecord Read Only Model · devroom.io

Posted viahttp://www.ruby-forum.com/.


Only two surfaces of a box:
http://blog.pixnet.net/zusocfc

Okay, I seeThanks, Matt.

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

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