Jeremy Luebke
2007-Aug-09 21:16 UTC
Keeping a record that belonged to a record that has been del
I have 2 tables. One for employees, the other for testimonials. An employee has_many testimonials and a testimonial belongs to an employee. Now when an employee leaves, we as a company want to keep the testimonial, but just not have it assigned to an employee. Problem is, if the employee is deleted form the table, and a testimonial still references that employee via employee_id, errors get thrown up. What would be a good solution for this? I thought about creating a blank employee with an id of 0. But that too could get deleted through the system. Is there a way to tell ruby a certain employee record is read only and can''t be deleted without having to write the restrict code over and over? Or maybe there is a way to creata a fake, static employee in the model? Thanks Jeremy -- 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 -~----------~----~----~----~------~----~------~--~---
jko170
2007-Aug-09 21:54 UTC
Re: Keeping a record that belonged to a record that has been del
could you do this? class Employee < ActiveRecord::Base has_many (or has_one) :testimonials, :dependent => :nullify end This will set the employee_id field to NULL in the testimonial table. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jeremy Luebke
2007-Aug-09 23:07 UTC
Re: Keeping a record that belonged to a record that has been
jko170 wrote:> could you do this? > > class Employee < ActiveRecord::Base > has_many (or has_one) :testimonials, :dependent => :nullify > end > > This will set the employee_id field to NULL in the testimonial table.From the API: If set to :nullify all associated objects'' foreign keys are set to NULL without calling their save callbacks. So wouldn''t that nulify every employee_id of the record in the testimonial table? Wouldn''t that make is so I can''t tell what employee a testimonial is assigned to? -- 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 -~----------~----~----~----~------~----~------~--~---
tobyhede
2007-Aug-09 23:59 UTC
Re: Keeping a record that belonged to a record that has been
The "nullify" is only set on the models associated with the deleted model, not all of the records in the associated table. And just a thought: Why delete the employee? Why not use some sort of status to indicate they are no longer currently employed? You can then handle the display of employees based on status, and can easily collect testimonials from all employees who are no longer current. Just a thought. Toby Hede Web Application Development Melbourne, Australia =================http://info-architects.net/ On Aug 10, 9:07 am, Jeremy Luebke <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> jko170 wrote: > > could you do this? > > > class Employee < ActiveRecord::Base > > has_many (or has_one) :testimonials, :dependent => :nullify > > end > > > This will set the employee_id field to NULL in the testimonial table. > > From the API: > If set to :nullify all associated objects'' foreign keys are set to NULL > without calling their save callbacks. > > So wouldn''t that nulify every employee_id of the record in the > testimonial table? Wouldn''t that make is so I can''t tell what employee a > testimonial is assigned to? > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Jeremy Luebke
2007-Aug-10 00:56 UTC
Re: Keeping a record that belonged to a record that has been
tobyhede wrote:> The "nullify" is only set on the models associated with the deleted > model, not all of the records in the associated table.OK that makes a lot more sense. Thanks, It works.> And just a thought: > Why delete the employee? > Why not use some sort of status to indicate they are no longer > currently employed?There is a bool for active or not which defines if they stay, but this is in an industry with very high turnover, so they have to be deleted for organizational reasons. Thanks Jeremy -- 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 -~----------~----~----~----~------~----~------~--~---