I want one record in my table to be undeletable by a user. I guess/know i can achieve this by writing code in my controller and model to only present the user with reocords which can be deleted but im wondering if there are any magic rails methods that can do this in a concrete way? background: Im writing a todolist which has two tables, tasks and projects whih have a standard has_many relationship. ie. a project has many tasks. When a user creates a task they specify whihc project they want to add it to. If a task is just a one off and not aprt of any project the user can specify "No project" in teh project dropdown. this in fact is a project but just using it to create the illusion! this is the record i dont want the user to be able to delete. -- 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 -~----------~----~----~----~------~----~------~--~---
On Thu, Jan 22, 2009 at 2:28 PM, Adam Akhtar < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I want one record in my table to be undeletable by a user. I guess/know > i can achieve this by writing code in my controller and model to only > present the user with reocords which can be deleted but im wondering if > there are any magic rails methods that can do this in a concrete way? > > background: Im writing a todolist which has two tables, tasks and > projects whih have a standard has_many relationship. ie. a project has > many tasks. > > When a user creates a task they specify whihc project they want to add > it to. > If a task is just a one off and not aprt of any project the user can > specify "No project" in teh project dropdown. this in fact is a project > but just using it to create the illusion! this is the record i dont want > the user to be able to delete. > -- > Posted via http://www.ruby-forum.com/. > > > >Set the model readonly class MyModel < ActiveRecord::Base def initialize(*args) super(*args) readonly! end end -- Andrew Timberlake http://ramblingsonrails.com http://www.linkedin.com/in/andrewtimberlake "I have never let my schooling interfere with my education" - Mark Twain --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Andrew Timberlake wrote:> Set the model readonly > > class MyModel < ActiveRecord::Base > def initialize(*args) > super(*args) > readonly! > end > endWouldn''t this make all records of the table read only? I don''t think that''s what the OP wanted. I''d say use the the before_destroy callback to validate the deletion: def before_destroy !project_name.eql?("No project") end -- 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 -~----------~----~----~----~------~----~------~--~---
Robert Walker wrote:> I''d say use the the before_destroy callback to validate the deletion: > > def before_destroy > !project_name.eql?("No project") > endOn second thought this might be a better approach: class Project < ActiveRecord::Base before_destroy :protect_no_project private def protect_no_project !name.eql?("No project") end end It sounds like overriding the callback directly may not be the best approach. -- 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 -~----------~----~----~----~------~----~------~--~---
Thanks everyone for your input. Yes I did only want to protect one of the records so ill give your (roberts) method a shot! thank you very much -- 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 -~----------~----~----~----~------~----~------~--~---