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! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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: http://ariejan.net/2008/08/17/activerecord-read-only-models/ -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 3月5日, 下午1時12分, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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: > > http://ariejan.net/2008/08/17/activerecord-read-only-models/ > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 5, 6:11 am, CFC <zuso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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> On 3月5日, 下午1時12分, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > 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: > > >http://ariejan.net/2008/08/17/activerecord-read-only-models/ > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 :D On Thu, Mar 5, 2009 at 5:35 PM, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Mar 5, 6:11 am, CFC <zuso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > On 3月5日, 下午1時12分, Robert Walker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > > > > > 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: > > > > >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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 4, 11:13 pm, CFC <zuso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Okay, I seeThanks, Matt. On Fri, Mar 6, 2009 at 12:31 AM, Matt Jones <al2o3cr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Mar 4, 11:13 pm, CFC <zuso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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... > > >-- Only two surfaces of a box: http://blog.pixnet.net/zusocfc --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---