I have the following code (relevant section offset with dashes): def create @post = Post.new(params[:post]) @post.user_id = session[:user].id @post.conversation_id = params[:conversation_id] @conversation = Conversation.find(params[:conversation_id]) replies = @conversation.replies + 1 @posts = Post.find(:all, :conditions => ["conversation_id = ?", params[:conversation_id]]) ------------------------------------------------------------------------------------------------------ @user = User.find(session[:user].id) @user.num_posts = session[:user].num_posts + 1 if @post.save Conversation.update(params[:conversation_id], { :updated_at => Time.now, :replies => replies }) User.update(session[:user].id, { :num_posts => (session[:user].num_posts += 1) }) @user.update session[:user] = @user @posts << @post render :partial => "conversations/post" else ------------------------------------------------------------------------------------------------------ render :action => ''new'' end end For reasons I can''t explain two odd things are happening. Firstly, the User.update line is never executed (at least no update on User shows in the log). Secondly after adding the @user code to increment post count it increments by 3 with each post. I did some playing around and if I add @user.num_posts -= 2 after @user.num_posts session[:user].num_posts + 1 it will increment by two. Yeah, after a net change of -1 in the value the total becomes 2 larger than it was. I must be missing something really, really obvious but I can''t see it. Help please!? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> > if @post.save > Conversation.update(params[:conversation_id], { :updated_at => > Time.now, :replies => replies }) > User.update(session[:user].id, { :num_posts => > (session[:user].num_posts += 1) })I assume the Conversation.update line is being called? i.e. that @post.save is successful and it actually enters the if statement? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Well, is there a reason for updating user twice? Aren''t session[:user] and @user supposed to represent the same object? If you''re doing what I think you are, shouldn''t you set @user Users.find(session[:user].id), then operate only on @user- I wouldn''t store a user object in the session like that, and do work on it.>>User.update(session[:user].id, { :num_posts => (session[:user].num_posts += 1) }) >>@user.updateOn Jan 30, 2:09 pm, Glen <DamnBig...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have the following code (relevant section offset with dashes): > > def create > @post = Post.new(params[:post]) > @post.user_id = session[:user].id > @post.conversation_id = params[:conversation_id] > > @conversation = Conversation.find(params[:conversation_id]) > > replies = @conversation.replies + 1 > > @posts = Post.find(:all, :conditions => ["conversation_id = ?", > params[:conversation_id]]) > > ------------------------------------------------------------------------------------------------------ > > @user = User.find(session[:user].id) > @user.num_posts = session[:user].num_posts + 1 > > if @post.save > Conversation.update(params[:conversation_id], { :updated_at => > Time.now, :replies => replies }) > User.update(session[:user].id, { :num_posts => > (session[:user].num_posts += 1) }) > @user.update > session[:user] = @user > @posts << @post > render :partial => "conversations/post" > else > > ------------------------------------------------------------------------------------------------------ > > render :action => ''new'' > end > end > > For reasons I can''t explain two odd things are happening. Firstly, > the User.update line is never executed (at least no update on User > shows in the log). Secondly after adding the @user code to increment > post count it increments by 3 with each post. I did some playing > around and if I add @user.num_posts -= 2 after @user.num_posts > session[:user].num_posts + 1 it will increment by two. Yeah, after a > net change of -1 in the value the total becomes 2 larger than it was. > > I must be missing something really, really obvious but I can''t see it. > > Help please!?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
On Jan 30, 1:24 pm, Phil Tayo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > if @post.save > > Conversation.update(params[:conversation_id], { :updated_at => > > Time.now, :replies => replies }) > > User.update(session[:user].id, { :num_posts => > > (session[:user].num_posts += 1) }) > > I assume the Conversation.update line is being called? i.e. that > @post.save is successful and it actually enters the if statement? > -- > Posted viahttp://www.ruby-forum.com/.Yeah sorry should have mentioned that. It is only being called once too as the conversation count increments by one with each post. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 30, 1:28 pm, Glen <DamnBig...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jan 30, 1:24 pm, Phil Tayo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > > if @post.save > > > Conversation.update(params[:conversation_id], { :updated_at => > > > Time.now, :replies => replies }) > > > User.update(session[:user].id, { :num_posts => > > > (session[:user].num_posts += 1) }) > > > I assume the Conversation.update line is being called? i.e. that > > @post.save is successful and it actually enters the if statement? > > -- > > Posted viahttp://www.ruby-forum.com/. > > Yeah sorry should have mentioned that. It is only being called once > too as the conversation count increments by one with each post.That did it. I didn''t realize that when I put an object into the session it gets looked up constantly. Any idea why the User.update never runs? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---