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:

[code] 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 [/code]

But, it won't create any events but the auto_increment will be increment. Do anybody knows how to solve it?

Thanks!

Billy Hsu 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:

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:

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...

Okay, I see

Thanks, Matt.