# HAS\_MANY BELONGS\_TO noobie question

**URL:** <https://www.ruby-forum.com/t/has-many-belongs-to-noobie-question/185854>\
**Category:** Rails\
**Created:** [March 27, 2010, 3:08pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-noobie-question/185854 "2010-03-27T15:08:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [March 27, 2010, 3:08pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-noobie-question/185854/1 "2010-03-27T15:08:08Z")

</div>

Hey,

I made my authentication system using Authlogic, and I have a  
current\_user helper

Now, I also made a scaffold called posts, and everytime a user creates  
a post using that scaffold, i want it to be assigned to that  
particular user.

So i can render out the posts that ONLY that user made.

Now, the question is what to do in the forms, and what to do in the  
posts controller.

Right now, I already have a has\_many belongs\_to relationship between  
the too, but i dont know where to go next. What do i have to chnage in  
the posts controller? What is my post form supposed to look like in  
order for it to be associated with the current\_user?

AWww ive been trying to get my mind around this, but i can’t. i need  
help. really bad.

Someone please help. please!!

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [March 27, 2010, 3:28pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-noobie-question/185854/2 "2010-03-27T15:28:02Z")

</div>

Hey David,

It’s really pretty easy and straightforward.  
In your PostsController:

def create  
@post = current\_user.posts.build(params[:post])  
if @post.save  
# …  
else  
# …  
end  
end

Also in your PostsController, to list posts only from current\_user:

def index  
@posts = current\_user.posts  
end

If you want your show method to only look for posts from current\_user:

def show  
@post = current\_user.posts.find(params[:id])  
end

/Lasse

2010/3/27 David Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [March 28, 2010, 4:14am UTC](https://www.ruby-forum.com/t/has-many-belongs-to-noobie-question/185854/3 "2010-03-28T04:14:25Z")

</div>

horrah! thanks so much, it was the @post =  
current\_user.posts.build(params[:post]) that i was stuck on

And now, i get it. and now it works. ur a life saver, thanks so  
much !!!

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 3, 2010, 12:54am UTC](https://www.ruby-forum.com/t/has-many-belongs-to-noobie-question/185854/4 "2010-04-03T00:54:42Z")

</div>

bump… 🙂 i really need help guys.

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 2, 2010, 6:41pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-noobie-question/185854/5 "2010-04-02T18:41:11Z")

</div>

Oh, thanks that relaly helped, but one more thing-

If i assigned posts to anotherposts, through a has\_many :anotherposts  
and belongs\_to :posts relationship, how would i do @post =  
current\_user.posts.build(params[:post]) in this situation? Because i  
dont have the current\_user helper anymore, because we are not dealing  
with users, but with the post that i want to create anotherposts ontop  
of.

so basically instead of the current\_user helper, i want to use the  
post that the anotherposts is being created on.

Do you understand? Hit me with more questions if you want 🙂

I really need help, this shouldn’t be difficult at all. Just a minor  
switch, that i can’t get my mind around. Hope you can help! thanks!!
