Displaying 20 results from an estimated 300 matches similar to: "before_destroy not called"
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 how to write the test for it
--
Posted via
2008 May 18
0
Spec a before_destroy callback in Sequel
Hi
I''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
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!"
return false
end
end
When I remove the dependent
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 simplest
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
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
2007 Jun 29
1
Speeding up :dependent => :destroy
I have a tree of models that represent a book. Looks like this:
book
sections
documents
paragraphs
index entries
glossary entries
footnotes
index entries
glossary entries
These are all models with has_many from parent and the child has
belongs_to. They also all have dependent => :destroy
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
2006 Mar 26
4
A unit test that should pass
Hi, I wonder why this unit test fails.
The model :
class Article < ActiveRecord::Base
set_table_name "publish_articles"
belongs_to :category
validates_presence_of :title, :excerpt
#snip
end
The test :
def test_validate
@article.title = nil
@article.excerpt = nil
assert !@article.save
assert_equal 2, !@article.errors.count
end
!@article.errors.count returns
2006 Aug 01
3
Validation on ActiveRecord destruction
Rails has many built in validation methods to ensure that an
ActiveRecord instance cannot be created or updated under certain
situations (typically 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
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/.
2006 Mar 13
6
:dependent => :destroy
Hi,
There is something I don''t understand about :dependent => :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
2009 Apr 02
3
error_messages_for does not display the error
Hi all
I''ve approached Rails since a couple of months to develop a quick
application for 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
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 ||
2008 Mar 17
4
RSpec''ing model association callbacks
Hi all,
i''m learning rspec and i must admit i really love it.
But at the time i started learning 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
2006 Jun 23
10
Don''t un-admin the last administrator
I have a User class with a field called admin which is a boolean that
determines if the user is or is not an administrator. I want to make it
impossible for the last administrator for an account to be removed from
the system.
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
2005 Dec 29
5
Extracting SQL and Rebuilding from SQL?
Hello-
With a database filled with several customers'' datasets, I thought it
would be a nice feature of my app''s backend interface to be able to
extract (and optionally delete) the data from a single customer. It
would then also be nice to rebuild that data from the stored info.
What''s a good way to do this efficiently?
If all the ":dependent => true"
2006 Apr 01
2
ActiveRecord callbacks not called
Hi, there must be something wrong somewhere in my unit test because
there''s a model I can''t have any callback executed on.
In my unit test I do
@image = Image.new
@image.set_uploaded_file({:file =>
uploaded_file("pic1.jpg","image/jpeg","article-picture.jpg"),
:article_id => @article.id,
:title => ''Titre
2006 Jul 18
2
Testing which RuntimeError is raised
I am just starting to use unit testing. Way cool. But I am not finding
very clear docs on assert_raise. I have managed to write a test that
tells me that the destroy I am trying to test fails with a
RuntimeError (as it should). However, I would really like to test that
it rails for the specific reason I have in my model. How can I get
more specific? Am I going to have to create my own exception
2005 Nov 26
3
Several questions about Ferret.
Hi.
First of all I would like to say "thank you" to David for its really
valuable work. Ferret is a great project and it have great future.
Well now is my questions as beginner in Ferret.
How to remove ALL documents from index. Remove files is not a solution. I am
interesting in something like
index.remove_index or something like this. What is a usual way of doing it??
What is the