search for: before_add

Displaying 7 results from an estimated 7 matches for "before_add".

2010 Jul 12
1
How to turn :before_add exceptions into validation errors
As per http://rails.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html Possible callbacks are: before_add, after_add, before_remove and after_remove. Should any of the before_add callbacks throw an exception, the object does not get added to the collection. Same with the before_remove callbacks; if an exception is thrown the object doesn‘t get removed. However, when the add fails due to a before_add...
2006 Apr 05
4
validation on before_add callback
problem: validation error is not functionning in callback callback for many-to-many association: # keyword.rb :before_add => :reject_self_related def reject_self_related(related_keyword) if (related_keyword == self) # this does not work: errors.add(:name, "Relating a keyword to itself is forbidden!") raise "Relating a keyword to itself is forbidden!"...
2007 Sep 07
5
enforce mutual exclusivity
Hi, First post, please be gentle! I couldn''t find any mention of this already I''m using ActiveRecord and I want to enforce mutual exclusivity on a has_and_belongs_to_many. A concrete example: I have a User which can have one or more Roles (student, tutor, headmaster, administrator etc). However if a user is a student they cannot hold any other role. I was hoping to find
2008 Aug 25
2
Validating an association
I have two models, A and B, related to one another by a many to many association and I want to validate an object of model B as it''s being added to the association based on both its own attributes and attributes of the particular object of model A that it''s being associated with. I''ve realized that I can''t do this if the association is represented by a habtm
2006 Jun 14
3
Prevent duplicate HABTM associations
...:uniq=>true on the model association but this would only prevent it from displaying duplicates, I want to stop duplicate associations being inserted in the first place. I have a vague idea of something using a callback to check before adding. has_and_belongs_to_many :catalogue_categories, :before_add => :evaluate_duplicate_category def evaluate_duplicate_category(catalogue_category) #check duplicate #if so raise error and abort add end However I am unsure how to abort the add at this stage and return an error. Could anyone help point me in the right direction? Thanks, Andrew....
2006 Apr 30
1
Callbacks on has_many relation
Hello, Is there a way to implement a callback for somehting like before_add and after_ad on a statement like this with a has_many relation? x.children << Item.create(:title=>"test") I want that x has a callback that is called when the item is added. I found something in C:\ruby\lib\ruby\gems\1.8\gems\activerecord-1.14.0\lib\active_record\association...
2006 Jan 31
2
How can I overwrite the parent.children.push(child) Method?
Hi all I want to overwrite the push method (which is an alias of <<, the same as concat) of collections, and there I want to test if :uniq is set to true in the relationship. If so, the method should check if the passed object is already related to the parent, or not (only then it will be added). But I just can''t find the original code of this method, so I could overwrite