has_many and belongs_to

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

<snip>

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*?

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 %>   <hr />   <% 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