# New to Rails: Assigning Post to Users

**URL:** <https://www.ruby-forum.com/t/new-to-rails-assigning-post-to-users/67416>\
**Category:** Rails\
**Created:** [September 4, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/new-to-rails-assigning-post-to-users/67416 "2006-09-04T14:43:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Curmorpheus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aeb1de/32.png) [@Curmorpheus](https://www.ruby-forum.com/u/Curmorpheus)\
**Post date:** [September 4, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/new-to-rails-assigning-post-to-users/67416/1 "2006-09-04T14:43:26Z")

</div>

First off, I’m new to rails - this is only my second day playing with  
it. Here’s my problem.

I have a post model and a user model. I want to have a has\_one  
relationship between posts and users. Meaning a post ‘has\_one’ user  
(author). I feel like I’m doing things right in the model but I’m just  
not understanding the “for” loops yet. Take a look at the show.rhtml to  
see my messed up for loop.

Any help is appreciated, here is some of the code

* * *

## V--------show.rhtml--------V

### Author
\<% for user in @post.users %\> \<%= user.username %\> 
* * *
\<% end %\>
* * *

## V--------post.rb--------V

class Post \< ActiveRecord::Base  
validates\_presence\_of :title  
has\_many :comments  
has\_one :user  
end

* * *

## V--------user.rb--------V

class User \< ActiveRecord::Base  
end

* * *

## V--------rails error page--------V

undefined method `users’ for #Post:0x36edc34

Extracted source (around line #7):

4: \<%= link\_to ‘Back’, :action =\> ‘list’ %\>  
  
5:  
6:

### Author
  
7: \<% for user in @post.users %\>  
8: \<%= user.username %\>  
9: 
* * *
  
10: \<% end %\>

---

<div class="post-metadata">

**Author:** ![Curmorpheus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aeb1de/32.png) [@Curmorpheus](https://www.ruby-forum.com/u/Curmorpheus)\
**Post date:** [September 4, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/new-to-rails-assigning-post-to-users/67416/2 "2006-09-04T14:43:29Z")

</div>

> I have a post model and a user model. I want to have a has\_one  
> relationship between posts and users. Meaning a post ‘has\_one’ user  
> (author). I feel like I’m doing things right in the model but I’m just  
> not understanding the “for” loops yet. Take a look at the show.rhtml to  
> see my messed up for loop.  
> Ah - you may be getting slightly mixed up. A post belongs to a user and  
> a user has many posts. This One to Many relationship should be defined  
> like this:

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

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

has\_one is used for a One to One relationship - like that between, for  
example, orders and invoices. In this case an order has one invoice and  
an invoice has one order.

Hope that helps,

Steve

---

<div class="post-metadata">

**Author:** ![Curmorpheus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aeb1de/32.png) [@Curmorpheus](https://www.ruby-forum.com/u/Curmorpheus)\
**Post date:** [September 4, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/new-to-rails-assigning-post-to-users/67416/3 "2006-09-04T14:43:34Z")

</div>

Curmorpheus \<curt.mills@…\> writes:

> I have a post model and a user model. I want to have a has\_one  
> relationship between posts and users. Meaning a post ‘has\_one’ user  
> (author). I feel like I’m doing things right in the model but I’m just  
> not understanding the “for” loops yet. Take a look at the show.rhtml to  
> see my messed up for loop.

It doesn’t sound like you need a loop at all - since a post only has one  
user,  
you just call post.user

The Rails magic here happens when you put “has\_one :user” in the model -  
that  
line creates the .user and .user= methods among others. If you’d used  
“has\_many  
:users” then a different set of methods would have been created,  
including .users

[http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html](http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html)  
has more info.

Gareth
