I''ve a Report and ReportType. The Report has one ReportType. Now I want prevent deleting of used ReportTypes. This is my ReportType class: class ReportType < ActiveRecord::Base validates_presence_of(:name); has_many :reports end And my Report class: class Report < ActiveRecord::Base belongs_to :report_type, :class_name => "ReportType", :foreign_key => "report_type_id" end I can create a query to find related reports, but I don''t think that''s the right way. Can anyone help? -- 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 -~----------~----~----~----~------~----~------~--~---
> I can create a query to find related reports, but I don''t think that''s > the right way. Can anyone help?That would be one option. Do that in an before_destroy callback. Option number two: use a counter_cache. Add a column report_count to ReportType and define belongs_to :report_type, :class_name => "ReportType", :foreign_key => "report_type_id", :counter_cache => true Then, again in before_destroy you can check for the number of records without an extra query (and comes in useful in many other situations) -- 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 -~----------~----~----~----~------~----~------~--~---
> Then, again in before_destroy you can check for the number of records > without an extra query (and comes in useful in many other situations)But can I also place a restrict on delete in my database and catch that error? -- 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 23 May 2008, at 09:57, Sjoerd Schunselaar wrote:> >> Then, again in before_destroy you can check for the number of records >> without an extra query (and comes in useful in many other situations) > > But can I also place a restrict on delete in my database and catch > that > error?Yes, create an actual foreign key constraint. rails won''t help you there, but you can execute arbitrary sql in your migrations (or anywhere else in fact), so just lookup foreign keys in the documentation of your database. Fred> > -- > 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 -~----------~----~----~----~------~----~------~--~---