# Models and relationships

**URL:** <https://www.ruby-forum.com/t/models-and-relationships/89006>\
**Category:** Rails\
**Created:** [March 16, 2007, 1:19pm UTC](https://www.ruby-forum.com/t/models-and-relationships/89006 "2007-03-16T13:19:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![revlob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c77e96/32.png) [@revlob](https://www.ruby-forum.com/u/revlob)\
**Post date:** [March 16, 2007, 1:19pm UTC](https://www.ruby-forum.com/t/models-and-relationships/89006/1 "2007-03-16T13:19:49Z")

</div>

If I have three models; Post, Comment, and User, and they are all set up  
according to Rails naming conventions, and I have established their  
relationships in the model code as follows:

class Post \< ActiveRecord::Base  
has\_many :comments  
end

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

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

Then should I be able to do something like this?

01 \<% for post in @posts %\>  
02 \<% for comment in post.comments %\>  
03 \<%= comment.user.username %\>  
04 \<% end %\>  
05 \<% end %\>

It’s line 03 that’s causing me a problem, as it generates an “undefined  
method ‘user’ for #Comment:0xHEX” error. Replacing that line with  
User.find(comment.user\_id).username works, but Im not sure if I should  
be doing it that way or not.

---

<div class="post-metadata">

**Author:** ![revlob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c77e96/32.png) [@revlob](https://www.ruby-forum.com/u/revlob)\
**Post date:** [March 16, 2007, 1:20pm UTC](https://www.ruby-forum.com/t/models-and-relationships/89006/2 "2007-03-16T13:20:40Z")

</div>

David E. wrote:

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

Ignore my poor copy and pasting, that should be class Comment.

---

<div class="post-metadata">

**Author:** ![revlob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c77e96/32.png) [@revlob](https://www.ruby-forum.com/u/revlob)\
**Post date:** [March 16, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/models-and-relationships/89006/3 "2007-03-16T15:45:16Z")

</div>

David E. wrote:

> class Post \< ActiveRecord::Base  
> belongs\_to :post  
> belongs\_to :user  
> end
> 
> Then should I be able to do something like this?
> 
> 01 \<% for post in @posts %\>  
> 02 \<% for comment in post.comments %\>  
> 03 \<%= comment.user.username %\>  
> 04 \<% end %\>  
> 05 \<% end %\>
> 
> It’s line 03 that’s causing me a problem, as it generates an “undefined  
> method ‘user’ for #Comment:0xHEX” error. Replacing that line with  
> User.find(comment.user\_id).username works, but Im not sure if I should  
> be doing it that way or not.

According to your model structure, each post has only one comment… so  
it’s post.comment not post.comments…

hope this helps…

ilan

---

<div class="post-metadata">

**Author:** ![revlob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c77e96/32.png) [@revlob](https://www.ruby-forum.com/u/revlob)\
**Post date:** [March 16, 2007, 3:47pm UTC](https://www.ruby-forum.com/t/models-and-relationships/89006/4 "2007-03-16T15:47:29Z")

</div>

Ilan B. wrote:

> David E. wrote:

> According to your model structure, each post has only one comment… so  
> it’s post.comment not post.comments…
> 
> hope this helps…
> 
> ilan

Whoops… missed the edit… sorry…
