I want to automate a validation on all (or almost all) my models. That validations should happen before every delete/destroy. The goal is to stop the user from deleting a record if it has any ''has*'' associations. I know that there are gems like ''acts_as_paranoid'' that will keep the record in the DB while marking it as ''deleted'', but that is not what I need here. I already have in a module the initial code that checks for association records. The code is posted below for critique and in case anybody is interested. The code might change, though, based on requirements. What I need is to know how to make all models run automatically the method (''dependencies_found?'' in the code below) whenever a record is deleted without having to go to all models and include the module manually. Any ideas? Thanks in advance. <code> module Dependable def dependencies_found? dependencies = self.class.reflect_on_all_associations.select {|a| a.macro.to_s[0, 3] == ''has''} dependencies.any? {|d| eval "!self.#{d.name}.blank?"} end end </code> -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
pepe wrote in post #955921:> I want to automate a validation on all (or almost all) my models. That > validations should happen before every delete/destroy. > > The goal is to stop the user from deleting a record if it has any > ''has*'' associations.Use foreign key constraints in the database for this. The Foreigner gem will help manage these constraints. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> Use foreign key constraints in the database for this. The Foreigner gem > will help manage these constraints.Yes, I think you''re right and going with Referential Integrity should be the way to go to make sure the rules are enforced at the DB level. Although, I still would like to know how I can accomplish what I was trying to do. I didn''t know about Foreigner and took a look. It seems that it is meant to be used only with MySQL and PostgreSQL. I am using MSSQL and have not found anything useful out there as gems/plugins go for this DB. Any ideas? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
pepe wrote in post #955955:>> Use foreign key constraints in the database for this. The Foreigner gem >> will help manage these constraints. > > Yes, I think you''re right and going with Referential Integrity should > be the way to go to make sure the rules are enforced at the DB level. > Although, I still would like to know how I can accomplish what I was > trying to do.With foreign key constraints. There is really no reason to do it any other way.> > I didn''t know about Foreigner and took a look. It seems that it is > meant to be used only with MySQL and PostgreSQL. I am using MSSQL and > have not found anything useful out there as gems/plugins go for this > DB. Any ideas?Yes, apparently you missed my announcement a couple months ago of my fork of Foreigner with MS SQL Server support: http://github.com/marnen/foreigner The gem is available as marnen-foreigner. I think sparkfly-foreigner also has MS SQL Server support. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 10-10-20 08:46 PM, pepe wrote:>> Use foreign key constraints in the database for this. The Foreigner gem >> will help manage these constraints. > > Yes, I think you''re right and going with Referential Integrity should > be the way to go to make sure the rules are enforced at the DB level. > Although, I still would like to know how I can accomplish what I was > trying to do. > > I didn''t know about Foreigner and took a look. It seems that it is > meant to be used only with MySQL and PostgreSQL. I am using MSSQL and > have not found anything useful out there as gems/plugins go for this > DB. Any ideas? >you can run a sql command in your migration to generate a foreign key constraint yourself, look at activerecord execute -- Kind Regards, Rajinder Yadav | DevMentor.org | Do Good! ~ Share Freely GNU/Linux: 2.6.35-22-generic Kubuntu x86_64 10.10 | KDE 4.5.1 Ruby 1.9.2p0 | Rails 3.0.1 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 10-10-21 06:31 AM, Rajinder Yadav wrote:> On 10-10-20 08:46 PM, pepe wrote: >>> Use foreign key constraints in the database for this. The Foreigner gem >>> will help manage these constraints. >> >> Yes, I think you''re right and going with Referential Integrity should >> be the way to go to make sure the rules are enforced at the DB level. >> Although, I still would like to know how I can accomplish what I was >> trying to do. >> >> I didn''t know about Foreigner and took a look. It seems that it is >> meant to be used only with MySQL and PostgreSQL. I am using MSSQL and >> have not found anything useful out there as gems/plugins go for this >> DB. Any ideas? >> > > you can run a sql command in your migration to generate a foreign key > constraint yourself, look at activerecord execute >this should give you an idea: http://fdietz.wordpress.com/2008/08/03/migrations-and-foreign-key-handling/ -- Kind Regards, Rajinder Yadav | DevMentor.org | Do Good! ~ Share Freely GNU/Linux: 2.6.35-22-generic Kubuntu x86_64 10.10 | KDE 4.5.1 Ruby 1.9.2p0 | Rails 3.0.1 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> > Yes, I think you''re right and going with Referential Integrity should > > be the way to go to make sure the rules are enforced at the DB level. > > Although, I still would like to know how I can accomplish what I was > > trying to do. > > With foreign key constraints. There is really no reason to do it any > other way.But it would provide me with knowledge of Rails internals I don''t have right now that might become useful in the future.> Yes, apparently you missed my announcement a couple months ago of my > fork of Foreigner with MS SQL Server support:http://github.com/marnen/foreigner > The gem is available as marnen-foreigner.Thanks, will look into it. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
> > you can run a sql command in your migration to generate a foreign key > > constraint yourself, look at activerecord execute > > this should give you an idea: > > http://fdietz.wordpress.com/2008/08/03/migrations-and-foreign-key-han...That''s exactly what I did yesterday when I realized Foreigner worked only with MySQL and PostgreSQL and got it working but Foreigner seems to help you along the way and make things easier and more ''Railsy'', which was what I was looking for. Thanks for the input, though. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rajinder Yadav wrote in post #956033:> On 10-10-20 08:46 PM, pepe wrote: >> have not found anything useful out there as gems/plugins go for this >> DB. Any ideas? >> > > you can run a sql command in your migration to generate a foreign key > constraint yourself, look at activerecord executeBut don''t, because it will not be DB-agnostic and it will not appear in the schema file. Foreigner is your best bet.> > -- > Kind Regards, > Rajinder Yadav | DevMentor.org | Do Good! ~ Share Freely >Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
pepe wrote in post #956059:>> > Yes, I think you''re right and going with Referential Integrity should >> > be the way to go to make sure the rules are enforced at the DB level. >> > Although, I still would like to know how I can accomplish what I was >> > trying to do. >> >> With foreign key constraints. There is really no reason to do it any >> other way. > > But it would provide me with knowledge of Rails internals I don''t have > right now that might become useful in the future.Well, looking through the source code is never a bad thing. Just bear in mind that the DB is in the best position to do data integrity checking.> >> Yes, apparently you missed my announcement a couple months ago of my >> fork of Foreigner with MS SQL Server support:http://github.com/marnen/foreigner >> The gem is available as marnen-foreigner. > > Thanks, will look into it.Let me know if you have any problems with it. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> Yes, apparently you missed my announcement a couple months ago of my > fork of Foreigner with MS SQL Server support:http://github.com/marnen/foreigner > The gem is available as marnen-foreigner.Got it working. Thanks for the tip. It would be great if ''schema.rb'' got updated with the code for the FK constraints, though. I found this (https://rails.lighthouseapp.com/projects/8994/tickets/ 4347-add-foreign-key-support-to-migrations-and-schemarb-dump) from the original creator of the gem you forked but I''m not sure how I would go about using the feature. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
pepe wrote in post #956233:>> Yes, apparently you missed my announcement a couple months ago of my >> fork of Foreigner with MS SQL Server support:http://github.com/marnen/foreigner >> The gem is available as marnen-foreigner. > > Got it working. Thanks for the tip. It would be great if ''schema.rb'' > got updated with the code for the FK constraints, though.That''s what Foreigner does. Is it not working for you? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
On Thu, Oct 21, 2010 at 8:43 AM, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Rajinder Yadav wrote in post #956033: >> On 10-10-20 08:46 PM, pepe wrote: >>> have not found anything useful out there as gems/plugins go for this >>> DB. Any ideas? >>> >> >> you can run a sql command in your migration to generate a foreign key >> constraint yourself, look at activerecord execute > > But don''t, because it will not be DB-agnostic and it will not appear in > the schema file. Foreigner is your best bet.Thanks for the info, took a look, did not know about Foreigner. Agree DB-agnostic is a good thing.> > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
> > Got it working. Thanks for the tip. It would be great if ''schema.rb'' > > got updated with the code for the FK constraints, though. > > That''s what Foreigner does. Is it not working for you?My mistake. I just saw them at the end of the file. Thanks for everything, this is going to help a lot. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.