user142019
user142019

Reputation:

Multiple has_manys of the same model

I have these models:

Person

has_many :messages_form_person, :foreign_key => :from_user_id, :class_name => :messages
has_many :messages_to_person,     :foreign_key => :to_user_id, :class_name => :messages

Message

belongs_to :to_person, :foreign_key     => :to_user_id, :class_name => :person
belongs_to :from_person, :foreign_key   => :to_user_id, :class_name => :person

And this view:

person#show

<% @person.messages_to_person.each do |message| %>
  <%=h message.title %>
<% end %>

But I get this error:

TypeError in People#show

Showing app/views/people/show.html.erb where line #26 raised:

can't convert Symbol into String

Extracted source (around line #26):

23:   <%=h @person.biography %>
24: </p>
25: 
26: <% @person.messages_to_person.each do |message| %>
27: 
28: <% end %>
29: 

I basically want it so that people can send each other messages.

Can anyone help me? Thanks.

Upvotes: 0

Views: 62

Answers (1)

Ju Nogueira
Ju Nogueira

Reputation: 8461

The problem is :class_name should be a string

:class_name => "Message"
:class_name => "Person"

Upvotes: 3

Related Questions