Won't save user

So I'm trying to create a facebooky kinda of site to learn rails. And so i have a User class and a micropost class. Those are simple and I know how that all works. The micropost points to its user and all that fun stuff. But then i have a feed controller where i want to display ALL the microposts and then I want to click on them to view them. I set up the feed controller to view them and all that works. But then I also want for any person to be able to "like" or "dislike" a micropost. The micropost class has a like and a dislike variable in them. So I defined a like and dislike action in the feed controller and set that all up. The way its looks is kinda like this.

def like     @post = Micropost.find(params[:id])     @user = User.find(@post.user_id)     respond_to do |format|       format.html     end     @user.increaseVariable(@user)     @post.like_action(@post)     @user.save     @post.save   end

The routes file looks like this

  match "feed/:id/like" =>"feed#like"   match "feed/:id/dislike" =>"feed#dislike"

To me, this should all work correctly. And it did when I did the methods in the console, so i'm not worried about that. The problem is that the post will save, but the user will not. And i'm not sure why. Any thoughts?

def like    @post = Micropost.find(params[:id])    @user = User.find(@post.user_id)    respond_to do |format|      format.html    end    @user.increaseVariable(@user)    @post.like_action(@post)    @user.save    @post.save end

The routes file looks like this

match "feed/:id/like" =>"feed#like" match "feed/:id/dislike" =>"feed#dislike"

To me, this should all work correctly. And it did when I did the methods in the console, so i'm not worried about that. The problem is that the post will save, but the user will not. And i'm not sure why. Any thoughts?

We'd need to see the increaseVariable and like_action methods to know for sure. Until then, change those save() calls to save!() calls and see what error gets thrown...

Couple of other things you may want to consider...

@post = Micropost.find(params[:id]) @user = User.find(@post.user_id)

could be re-written as:

@post = Micropost.find(params[:id]) @user = @post.user

Also, the following two lines don't make much sense to me...

@user.increaseVariable(@user) @post.like_action(@post)

Why would you call a method on an object and pass the object itself into the method? You already have access to the object as 'self' within the method... ???

And lastly, if you're updating an integer field in the Micropost table, don't set it and save it, but look at using increment_counter. Do NOT use incremement (it sets and saves). increment_counter will write an 'UPDATE ...' SQL clause that update the value in the database by one directly.

Good luck!

-philip

Hm, i totally forgot about the self. I'm still trying to learn ruby and everything. So the increaseVariable method looks like this. I just edited it so it could use self and so i could scale how much wanted to increase the variable.

  def increaseVariable(scale)    tempoints = 1 * scale     self.variable += tempoints     logger.debug "User variable is now #{user.variable}" end

so now this is what my controller looks like

def like     @post = Micropost.find(params[:id])     @user = @post.user_ids     respond_to do |format|       format.html     end     @user.increaseVariable(5)     @post.like_action     @user.save!     @post.save!   end

So when i run it, it says that there is an undefined method for "variable" but its up in the attr_accessible. What else could be wrong?

Does your users table have a 'variable' field? Once it's in there you don't need to have attr_accessible at all.

Also, you're going to have a problem if variable is nil as "nil += 5" will throw an error. So you need to either default it to zero in the database or look out for that in your model methods.

-philip

Yeah, the table includes everything needed.

def self.up     create_table :users do |t|       t.string :name       t.string :email       t.integer :variable       t.timestamps     end   end

And the variable isn't nil. I default it to 0 when i create the user. I'm still confused as to why this isn't working... Everything goes like it should but the user just will not save itself and update the variable. Can I use the update_attributes?

Yeah, the table includes everything needed.

def self.up create_table :users do |t| t.string :name t.string :email t.integer :variable t.timestamps end end

And the variable isn't nil. I default it to 0 when i create the user. I'm still confused as to why this isn't working... Everything goes like it should but the user just will not save itself and update the variable. Can I use the update_attributes?

It doesn't sound like it's the saving part that is the problem, it's how you're manipulating the object before hand. What is on the line that throws the exception? Your controller looks a bit weird too - @post.user_ids should return an array of integers (assuming post has_many users), so trying to call on that methods you've presumably defined on User doesn't make any sense

Fred

yeah, the @post.user_ids actually is a typo. i tried editing it but it looks like that didn't work... it really is just @post.user_id which is the user id that the post belongs to. I was using the debugger before i was saving the @user, and the @user.variable was different. but when i saved it nothing would save. The reason i'm confused is that the post will updates its likes but the user won't updates its variable.

yeah, the @post.user_ids actually is a typo. i tried editing it but it looks like that didn't work... it really is just @post.user_id which is the user id that the post belongs to. I was using the debugger before i was saving the @user, and the @user.variable was different. but when i saved it nothing would save. The reason i'm confused is that the post will updates its likes but the user won't updates its variable.

@post.user_id wouldn't work either - that's just an integer and won't have all the attributes your models have. It's also really helpful if the code posted here is actually what's in your app, if there are typos here that aren't in your app (and vice versa) it can be nearly impossible to work out what is going on.

Fred

def increaseVariable(scale) tempoints = 1 * scale self.variable += tempoints logger.debug "User variable is now #{user.variable}" end So when i run it, it says that there is an undefined method for "variable" but its up in the attr_accessible. What else could be wrong?

You'll get undefined method variable for nil because user.variable should be self.variable instead. There's no user in increaseVariable method.

Cheers! Arzumy