Rails select NoMethodError in Messages#new

257 views Asked by At

I'm trying to put on a select box the Users who will receive the message. What we have is one table named Messages, one named Users and one named Friendships, on message table we have user_id (sender) and friend_id (receiver) both foreign key to Users ID.

And this is the error returning: undefined method `friend_id' for #Message:0x56f6238

That been said, I'm trying to vinculate all Friendships entries with current_user id and then using friend_id to search on Users table all users that current_user is able to send message.

Any help with this error? I'm really new on Ruby On Rails development. If I'm missing some info, let me know. Thanks in advance

On our application, the new_message_path renders one partial which contains the following:

<%= form_for(@message) do |f| %>
  <% if @message.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@message.errors.count, "error") %> prohibited this message from being saved:</h2>

      <ul>
      <% @message.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label "Receiver" %><br />
    <%= f.select :friend_id, Message.all.collect {|friendships| [ friendships.friend_id ] }, { include_blank: true } %>
  </div><br>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And on our model named Message we have this:

class Message < ActiveRecord::Base
  attr_accessible :content, :friend_id
  validates_presence_of :content, :friend_id, presence => true

  belongs_to :user

end
0

There are 0 answers