# Has\_many and belongs\_to

**URL:** <https://www.ruby-forum.com/t/has-many-and-belongs-to/113222>\
**Category:** Rails\
**Created:** [August 28, 2007, 11:43pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to/113222 "2007-08-28T23:43:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 28, 2007, 11:43pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to/113222/1 "2007-08-28T23:43:25Z")

</div>

I have this table called users and another called hotelmemberships.  
Users has\_many hotelmemberships and hotelmemberships belongs\_to users.  
Pretty straightforward. But, when I try and have parts of a form  
auto-filled with the hotelmembership info, I should just be able to say:

\<% @user.hotelmemberships.comments.nil? %\>  
…  
\<% end %\>

This tests the comments to see if there are any. I have done this  
countless number of times, but this time it thinks that comments is a  
method and kicks back the error:

undefined method `comments’ for Hotelmembership:Class

Not sure what is going on, but I feel pretty confident that this code  
(as simple as it is) should work. Does anyone have any ideas of what is  
wrong? Thanks,

-Shandy

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 29, 2007, 12:03am UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to/113222/2 "2007-08-29T00:03:23Z")

</div>

Shandy N. wrote:

> I have this table called users and another called hotelmemberships.  
> Users has\_many hotelmemberships and hotelmemberships belongs\_to users.  
> Pretty straightforward. But, when I try and have parts of a form  
> auto-filled with the hotelmembership info, I should just be able to say:
> 
> \<% @user.hotelmemberships.comments.nil? %\>  
> …  
> \<% end %\>
> 
> This tests the comments to see if there are any. I have done this  
> countless number of times, but this time it thinks that comments is a  
> method and kicks back the error:
> 
> undefined method `comments’ for Hotelmembership:Class
> 
> Not sure what is going on, but I feel pretty confident that this code  
> (as simple as it is) should work. Does anyone have any ideas of what is  
> wrong? Thanks,
> 
> -Shandy

maybe

comment instead of comments

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 29, 2007, 12:57am UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to/113222/3 "2007-08-29T00:57:01Z")

</div>

2007/8/28, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> \<% @user.hotelmemberships.comments.nil? %\>  
> …  
> \<% end %\>

\> undefined method `comments' for Hotelmembership:Class \>

I am not sure about the \<% end %\>. Why do you use it? Do you use a  
block somewhere near it starting with _do_?

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 29, 2007, 2:12am UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to/113222/4 "2007-08-29T02:12:49Z")

</div>

Shandy N. wrote:

> I have this table called users and another called hotelmemberships.  
> Users has\_many hotelmemberships and hotelmemberships belongs\_to users.  
> Pretty straightforward. But, when I try and have parts of a form  
> auto-filled with the hotelmembership info, I should just be able to say:
> 
> \<% @user.hotelmemberships.comments.nil? %\>  
> …  
> \<% end %\>
> 
> This tests the comments to see if there are any. I have done this  
> countless number of times, but this time it thinks that comments is a  
> method and kicks back the error:
> 
> undefined method `comments’ for Hotelmembership:Class
> 
> Not sure what is going on, but I feel pretty confident that this code  
> (as simple as it is) should work. Does anyone have any ideas of what is  
> wrong? Thanks,

The problem is that you need to enumerate the hotelmemberships. I’m  
assuming each membership has comments. Use #collect or #detect or  
something to walk all the user’s hotelmemberships and ensure that none  
have any comments. But what you probably want to do is show the comments  
for each membership, which is another problem.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 29, 2007, 2:22am UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to/113222/5 "2007-08-29T02:22:07Z")

</div>

Hi Shandy,

You got a User Model:

class User \< ActiverRecord::Base  
has\_many :hotel\_memberships, :include =\> :hotel  
end

This actually generates the hotel\_memberships instance method within  
your user model that returns a collection of HotelMembership instances.

Here: A collection means an instance of Array.

Further you have you HotelMembership Model:

class HotelMembership \< ActiveRecord::Base  
belongs\_to :member, :class\_name =\> ‘User’, :foreign\_key =\> :member\_id  
belongs\_to :hotel  
has\_many :comments, :through =\> :hotel  
end

This also generates a _instance_ method called comments. This method is  
only avaiable for instances of HotelMembership (not Array). You need to  
access the individual elements of the Array which are instances of  
HotelMembership to access the instance method called comments.

As a result your code should look like:

\<% @user.hotel\_memberships.each do |membership| %\>  
\<%= membership.hotel.name %\>

* * *
 \<% unless membership.comments.empty? %\> consider using: render :partial =\> 'comment', :collection =\> membership.comments in this case you don't need the check above... \<% end %\> \<% end %\>

Regards  
Florian
