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,
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,
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.