From: Glen Holcomb Date: 2007-05-15T02:10:54+09:00 Subject: Re: Rails: Counter problem ------=_Part_155001_10686262.1179162653904 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi Adam, Wrong list, you will have far better luck here: http://groups.google.com/group/rubyonrails-talk On 5/14/07, Adam42Smith@gmail.com wrote: > > Hi. I have a problem with counters in Rails. The situation looks like > this: > -3 simple models > > class Forum < ActiveRecord::Base > has_many :posts > end > class Topic < ActiveRecord::Base > has_many :posts > end > class Post < ActiveRecord::Base > belongs_to :topic, :counter_cache => true > belongs_to :forum, :counter_cache => true > end > > - In migrations i have declared > t.column :posts_count, :integer, :default => 0 > for Forum and Topic > > - Now a simple action > > def some_action > @post = Post.new(params[:id]) > > @forum.posts << @post # The @forums.posts_count is up by 1 > @topic.posts << @post # The @topic.posts_count is _not_ up by 1 > end > > Both, the forum_id and topic_id are set correctly in @post, it saves > into the database and everything else is dandy. But why oh why doesn't > the @topic.posts_count increment ? (oh, obviously if I change the > order > of the two crucial lines the effect will be the same - the instance in > the second line won't get the posts_count incremented) > > Could anyone come up with a solution ? (apart from making the counters > 'by hand') > > -- > AdamS > > > -- "Hey brother christian with your high and mighty errand, Your actions speak so loud, I can't hear a word you're saying." -Greg Graffin (Bad Religion) ------=_Part_155001_10686262.1179162653904--