search for: before_destroy

Displaying 20 results from an estimated 54 matches for "before_destroy".

2006 Mar 24
2
before_destroy not called
Hello, I''m trying to intercept delete call in my model but before_destroy callback is never called... Somebody knows why ? Thanks The controller : def delete Image.delete(params[:id]) redirect_to :action => "list" end The model : class Image < ActiveRecord::Base set_table_name "publish_images" belongs_to :article before_destro...
2008 May 18
0
Spec a before_destroy callback in Sequel
...'m struggling to find a neat way to spec this. The Sequel ORM doesn''t support automatically destroying dependent models. It''s easy enough to do in a callback though: class Changeset < Sequel::Model has_many :migrations, :class => DataModel::Migration before_destroy do migrations.each { |m| m.destroy } end # ... end The problem is isolating it from the database to test. I want to know that when I send Changeset#destroy that each migration is destroyed. The implementation of Sequel::Model#destroy (from the RDoc) is this: # File sequ...
2008 Jul 02
2
How to write the method before_destroy and the test
How to write the method before_destroy? Here is my code: def before_destroy errors.add(:isdefault,"Can''t destroy default record") if self.isdefault return false end or I should write it like below? def before_destroy raise(Can''t destroy default record") if self.isdefault end and h...
2006 May 16
2
Validate Before Delete
I have an Order model with a ''status'' column. What I want to be able to do is validate that the status of the Order is not completed before it is allowed to be deleted. I tried the following method in my Model, but it appears that this only works for an :update or :create on the record. Anyone know how to do this, and if it should be in the model or the controller?
2006 Aug 01
3
Validation on ActiveRecord destruction
...ypically caused by invalid data). However, I can find no methods to govern whether an ActiveRecord instance can be destroyed. Essentially, I wish an error to be raised when a user tries to delete an ActiveRecord without first adhering to a certain set of conditions. I currently achieve this via before_destroy method, where I set a custom error for the ActiveRecord in question. I then persist the ActiveRecord across the session, so that I can redirect from ''delete'' to ''list'' as usual without losing the error in the process. To display the error, I use an overridden...
2007 Mar 14
0
before_destroy return false, but dependent are destroyed
I have a problem with before_destroy deleting by dependent records, even through I return flase. ... has_many :users, :class_name => ''Editor'', :as => :editable, :dependent => :destroy ... def before_destroy unless self.has_access?(99) errors.add_to_base "Destroy permissions error!&quot...
2006 Jun 29
1
before_destroy & verification
Hey, probably an easy question but, I''m trying to get a method to run "before_destory" for a model, and I want the method to throw an error, and not delete the object from the database, if a certain condition is held Obviously, I can just raise an error and rescue it... but for some reason I can''t get <% error_messages_for ''model" %> to catch
2005 Jul 24
0
cancelling before_destroy
According to the documentation, if I return false in a model''s before_destroy method then it should cancel the action. Although I know it is reaching and running this code, it deletes the object despite the "return false". I''m sure this were a bug it would have been noticed by now so I must be missing something but I cannot imagine what. Even in the si...
2006 Dec 07
2
validate_on_destroy ?
What''s the best way to validate_on_destroy. Rails only seems to include validate_on_create and validate_on_update methods. I don''t want to resort to doing: before_destroy do |j| raise "There is an error" if j.etc end Is there a way of finding out what action (create/update/destroy) is being performed if I use the ''validate'' method? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You rec...
2006 Nov 17
4
before_destroy and sessions
(Semi-newbie.) I want to ensure a user''s able to destroy only his own objects. I''ve set session info at login: session[:user_id] = user.id Now I try this in the model of my deletable objects: before_destroy :destroy_your_own def destroy_your_own raise "Can''t delete that!" unless session[:user_id] == self.user.id end which snags EVERY attempted destroy. What am I doing wrong? Better ideas? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~--...
2006 Jul 20
5
How can I make has_many prevent a delete that would lead to orphans?
e.g. class Asset < ActiveRecord::Base validates_presence_of :asset_number, :make, :model, :location, :name, :serial_number validates_numericality_of :asset_number validates_uniqueness_of :asset_number belongs_to :user belongs_to :location belongs_to :asset_type, :foreign_key => ''type_id'' end class Location < ActiveRecord::Base validates_presence_of :name
2006 Feb 18
5
don''t destroy last user
How would I stop the last user being deleted. The following code doesn''t work. before_destroy :dont_destroy_last_user # Don''t delete user if it the last one def dont_destroy_last_User raise "Can''t destroy last user" if User.length < 1 end -- Posted via http://www.ruby-forum.com/.
2007 Jun 29
1
Speeding up :dependent => :destroy
...s each index, glossary, footnote, which then finds each index and glossary. This takes a LONG time... finding, opening, deleting each record... thousands of database queries. I need a way to speed this up as it is non functional. Two ways I can see: Remove the dependent => destroy and make a before_destroy method which puts the child delete in a transaction, and then cascade that same before_destroy method in each subclass (feels like a bad hack). Create a stored procedure in Mysql to handle it (not portable). Has anyone else fixed this problem before? What is the best way to go here? Regards Mi...
2005 Oct 23
12
Showing a neat error message
Hi All, I''m trying to prevent users from deleting a folder that has contents like this: class Folder < ActiveRecord::Base has_many :myfiles has_many :folders belongs_to :folder validates_uniqueness_of :name, :scope => "folder_id" before_destroy :dont_destroy_folder_with_contents def dont_destroy_folder_with_contents if Folder.find(id) != nil || Myfile.find(id) != nil raise "Cannot delete this folder because it has contents" end end end This works fine: the folder''s not deleted. However I do not get the...
2006 Mar 13
6
:dependent => :destroy
...; :destroy. I hope someone can help me. Consider this code: class Folder < ActiveRecord::Base has_many :myfiles has_many :folders has_many :group_folders, :dependent => :destroy validates_uniqueness_of :name, :scope => "folder_id" validates_presence_of :name before_destroy :dont_destroy_folder_with_contents # Folders containing files or sub-folders # can not be deleted def dont_destroy_folder_with_contents if self.folders.length > 0 or self.myfiles.length > 0 raise "Can''t destroy folder with contents" end end end...
2008 Jul 04
2
How do change catalog before catalog is delted?
In my rails application,there are two models:post and catalog. One post has a catalog,and one catalog has many posts. When I delete a catalog,the posts belongs to the catalog will not be shown normal,for it''s catalog is no existed. Now I want to create(if the ''Defalut catalog'' is not existed) a ''Defalut catalog'' in catalogs table,and make the
2009 Apr 02
3
error_messages_for does not display the error
...my company. Fantastic framework. As every noob, I do have some gaps to cover. The one which is giving me a little frustration, generated by my lack of knowledge is as follows. I need to make sure the region object is not deleted if there are countries associated with it. I solved this by putting a before_destroy methon in the model, as follows: class Region < ActiveRecord::Base has_many :countries validates_presence_of :name validates_uniqueness_of :name def before_destroy unless countries.count == 0 errors.add_to_base "Cannot delete a region with countries associated"...
2006 Jun 23
10
Don''t un-admin the last administrator
...stem. I need to protect against this both when deleting a user and when editing a user as you can revoke a user''s administrator privileges via a form. User belongs_to :account. Account has_many :Users I was thinking that the best way to do this would involve either before_update and before_destroy or a validation. I think I know how to write the before_delete as that just involves knowing which user you are going to delete and checking if its account has more than one administrator. I am unsure how to write before_update as that requires not only knowing which user you are going to upd...
2008 Mar 17
4
RSpec''ing model association callbacks
...arning it, i already developed my models classes and their callbacks. Now i''m trying to get a 100% coverage of my code but i cannot reach it because i do not understand how to spec my callbacks. Look at this for example: ----------------- User Model class User < ActiveRecord::Base before_destroy :delete_associated_comments def delete_associated_comments comments = Comment.find_all_by_user_id(self.id) comments.each { |c| c.destroy } end end ----------------- User Spec describe User, " being deleted" do before(:each) do end it "should see del...
2005 Nov 26
3
Several questions about Ferret.
...e but it seems does not work correctly. Document indexed twice after object update. Could you help me to write right Rails hook methods?? def after_save index = FerretConfig::INDEX index.remove(self.id.to_s) index.update(self.id.to_s, self.to_document) index.optimize end def before_destroy index = FerretConfig::INDEX index.remove(self.id.to_s) index.optimize end def to_document doc = Document.new doc << Field.new(''id'', self.id.to_s, Field::Store::YES, Field::Index::UNTOKENIZED) doc << Field.new(''body_en'...