search for: after_add

Displaying 16 results from an estimated 16 matches for "after_add".

2007 Jul 21
3
unable to get "has_many :after_add" to work - need help
Can someone tell me what I''m doing wrong? I''ve spent way too much time on this -------------------------------------------------------------------------------------- class Picture < ActiveRecord::Base has_many :ratings, :after_add => :increment_rating, :after_remove => :decrement_rating def increment_members(rating) self.rating += rating.amount save! end end -------------------------------------------------------------------------------------- class Rating < ActiveRecord::Base belongs_to :picture end...
2006 Jun 23
0
adding action after a belongs_to assignment
What is the best way to add action after a belongs_to assignment? (Just like ''after_add'' in has_many assignment). I see that there is no callbacks for belongs_to. I have tried overriding parent= : alias :parent_before_mod= :parent= def parent=(the_parent) parent_before_mod = the_parent if save # my actions here end end but when I tried to assign parent, parent_...
2006 May 02
0
Self-referential MtoM implementation
...friends_people end end class Person < ActiveRecord::Base has_and_belongs_to_many :friends, :class_name => "Person", :join_table => "friends_people", :association_foreign_key => "friend_id", :foreign_key => "person_id", :after_add => :be_friendly_to_friend, :after_remove => :no_more_mr_nice_guy def be_friendly_to_friend(friend) friend.friends << self unless friend.friends.include?(self) end def no_more_mr_nice_guy(friend) friend.friends.delete(self) rescue nil end end -- Posted via http://ww...
2006 Apr 17
0
Pros/cons of doubling up in Self-Referential has_many via :through
...relationship is a reflexive one -- i.e. if person A has a relationship (of type i) with person B, then person B has a relationship (of type i) with person A. In Rails Recipes (and in my original HABTM setup), this was done by automatically adding a reverse row to the relationships table using :after_add (and removing it with :after_remove). Downside is that if the table has attributes (relationship_type, notes on relationship) this starts to get a bit messy, meaning that every time one is updated the reverse must be too. I could wrap this up in a transaction, but somehow it seems a bit ineleg...
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
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
2007 Feb 05
0
after_save and associations
It appears that when after_save is triggered, associations such as habtm haven''t been populated yet. Each association can have an :after_add callback, but my code needs to access multiple associations. Is there an explicit reason why after_save isn''t triggered after all of the associations have been populated? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Go...
2008 Dec 16
1
Callback when objects connect as a habtm relationship
If Product and Category models are in a habtm relationship, i.e. class Product < ActiveRecord::Base has_and_belongs_to_many :categories end class Category < ActiveRecord::Base has_and_belongs_to_many :products end I want a piece of code to be executed every time a product is connected to a category. Where do I put this code? Which callback (and on which model) will be triggered? Any
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 violation,...
2012 Jun 01
0
Association callbacks on model observers
Hello all first post so be kind, Is there a way to use association callbacks on model observers? For instance if I have a has_and_belongs_to_many association on my model I can add a private method for instance to be called on the :after_add, is there a way to move this to an observer I have created for this model? Thanks .FxN -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/...
2011 Aug 12
1
Getting nil value when setting an on Join model via a has_many :through relationship
Hi All, I''ve been trying to figure out what''s wrong here for a little over a day and figured i''d reach out to the ROR list. A pastie explains the code a bit better than directly in email: http://pastie.org/private/mvu0zr1xm18bsk6nfbyama Basically i have an Artist, and he has many Songs through a join model called Releases. An artist can be "featured" on a
2006 Oct 31
5
conditional boost? friends to come up at top of search...
Hey guys, im trying to get my friends to come up at the top of the act as ferret search. I would query the whole result set first, then move my friends to the top, but the thing is, Im paginating my results and use the offset and limit parameters in the multi_search() function. Anyone know how to do this? Thanks in advance... -- Posted via http://www.ruby-forum.com/.
2006 May 22
3
STI, HABTM & counter_cache
Hello world... I have an interesting issue that the online docs aren''t helping me with. In my app I have 4 models Item < ActiveRecord Deal < Item Product < Item Category < ActiveRecord Item has_and_belongs_to_many :categories... On each category record I''d like to maintain a product_count and deal_count to increase performance. This doesn''t appear
2006 Feb 27
4
2 belongs_to to the same parent table
Hello! I have 2 table: users and buddies User: id, name, ... Buddy: id, user_id, user_buddy_id, ... So if you have 2 users 1,jack and 2,fred and fred is a buddy of jack, there is a Buddy object: id=1, user_id=1, user_buddy_id=2 I can declare only one belongs_to in Buddy and one has_many in User. And there is conflict if I had the second one (the first one is discarded) class User has_many
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
2006 Jan 09
6
has_and_belongs_to_many :self
Hi all I got the following class: class Member < ActiveRecord::Base has_and_belongs_to_many :buddies, :class_name => ''Member'', :join_table => ''members_have_buddies'', :foreign_key => ''member_id'', :association_foreign_key =>