# Push question

**URL:** <https://www.ruby-forum.com/t/push-question/114054>\
**Category:** Rails\
**Created:** [September 7, 2007, 11:38am UTC](https://www.ruby-forum.com/t/push-question/114054 "2007-09-07T11:38:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mariko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mariko](https://www.ruby-forum.com/u/mariko)\
**Post date:** [September 7, 2007, 11:38am UTC](https://www.ruby-forum.com/t/push-question/114054/1 "2007-09-07T11:38:22Z")

</div>

In my app, a Post belongs\_to a user and a User has\_many posts.

Say I’d like to change the user the post belongs to.

post = Post.find(1)  
post.user \<\< anotheruser

… gives me an error, since post.user is already defined. It also seems  
as if changing the attribute user\_id in this manner:

post.update\_attributes(:user\_id =\> 1)

doesn’t help either.

What’s the best way to do this?

---

<div class="post-metadata">

**Author:** ![mariko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mariko](https://www.ruby-forum.com/u/mariko)\
**Post date:** [September 7, 2007, 11:51am UTC](https://www.ruby-forum.com/t/push-question/114054/2 "2007-09-07T11:51:17Z")

</div>

PLease provide more code to get help. Thanks. I don’t understand why do  
you need add a another user to post. If you want this, then the  
association is has\_and\_belongs\_to\_many or has\_many :through between the  
model users and posts.

---

<div class="post-metadata">

**Author:** ![mariko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mariko](https://www.ruby-forum.com/u/mariko)\
**Post date:** [September 7, 2007, 12:03pm UTC](https://www.ruby-forum.com/t/push-question/114054/3 "2007-09-07T12:03:21Z")

</div>

Nicolas Santa wrote:

> PLease provide more code to get help. Thanks. I don’t understand why do  
> you need add a another user to post. If you want this, then the  
> association is has\_and\_belongs\_to\_many or has\_many :through between the  
> model users and posts.

Sorry, what I mean is that I’d like to _change_ which user the post  
belongs to, not add other ones. Here’s more code:

class User \< ActiveRecord::Base  
has\_many :posts  
end

class Post \< ActiveRecord::Base  
belongs\_to :user  
end

in the db schema:

create\_table “posts”, :force =\> true do |t|  
t.column “user\_id”, :integer  
end

Thanks in advance for any help.

---

<div class="post-metadata">

**Author:** ![mariko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecc23a/32.png) [@mariko](https://www.ruby-forum.com/u/mariko)\
**Post date:** [September 7, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/push-question/114054/4 "2007-09-07T16:50:04Z")

</div>

Read the documentation for belongs\_to at [api.rubyonrails.com](http://api.rubyonrails.com)

Hint: you’re not pushing a value onto a collection, you are setting a  
value on at attribute.

ReinH

On Sep 7, 5:03 am, “Mariko C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
