This is what I have belongs_to :user, :counter_cache => true belongs_to :vote_item, :counter_cache => true belongs_to :vote_topic, :counter_cache => true ##User model def vote_for(vid, vtid) Vote.create(:user_id => self.id, :vote_item_id => vid, :vote_topic_id => vtid) end def cancel_vote(vid, vtid) Vote.find(:first, :select => "id", :conditions => [''vote_item_id = ? AND vote_topic_id = ? AND user_id = ?'', vid, vtid, self.id]).destroy end When a vote is created the votes_count in VoteItem and VoteTopic are updated correctly. But when a vote is destroyed or cancelled the counter is not decremented. Please help -- 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 Aug 30, 8:02 pm, badnaam <asitkmis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This is what I have > > belongs_to :user, :counter_cache => true > belongs_to :vote_item, :counter_cache => true > belongs_to :vote_topic, :counter_cache => true > > ##User model > > def vote_for(vid, vtid) > Vote.create(:user_id => self.id, :vote_item_id => > vid, :vote_topic_id => vtid) > end > > def cancel_vote(vid, vtid) > Vote.find(:first, :select => "id", :conditions => > [''vote_item_id = ? AND vote_topic_id = ? AND user_id = ?'', vid, vtid, > self.id]).destroy > end > > When a vote is created the votes_count in VoteItem and VoteTopic are > updated correctly. But when a vote is destroyed or cancelled the > counter is not decremented. >Stab in the dark: because your select clause only includes id, rails doesn''t know the user_id/vote_item_id/vote_topic_id and so doesn''t know which user/vote_item/vote_topic to update. Fred> Please help-- 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.
Bulls eye! That was it On Aug 30, 12:16 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Aug 30, 8:02 pm, badnaam <asitkmis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > This is what I have > > > belongs_to :user, :counter_cache => true > > belongs_to :vote_item, :counter_cache => true > > belongs_to :vote_topic, :counter_cache => true > > > ##User model > > > def vote_for(vid, vtid) > > Vote.create(:user_id => self.id, :vote_item_id => > > vid, :vote_topic_id => vtid) > > end > > > def cancel_vote(vid, vtid) > > Vote.find(:first, :select => "id", :conditions => > > [''vote_item_id = ? AND vote_topic_id = ? AND user_id = ?'', vid, vtid, > > self.id]).destroy > > end > > > When a vote is created the votes_count in VoteItem and VoteTopic are > > updated correctly. But when a vote is destroyed or cancelled the > > counter is not decremented. > > Stab in the dark: because your select clause only includes id, rails > doesn''t know the user_id/vote_item_id/vote_topic_id and so doesn''t > know which user/vote_item/vote_topic to update. > > Fred > > > Please help > >-- 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.