2

So, I entirely understand why one would normally receive an undefined method error, however, I am unsure why it is happening this particular time.

I have a partial:

submissions/_submission.html.erb

<div class="pure-u-1-2 rounded">
 <%= link_to submission do %>
  <div class="rounded" style="background-color: #B81324; color: #E6E6E6; margin: 10px; padding:5px;">
  <div class="clear">Project: <%= submission.project.title %></div>
  <div class="clear">Submitted By: <%= submission.submitter.username %></div>
  <div class="clear">Amount: $<%= submission.price %></div>
  <div class="clear">Description: <%= submission.description %></div>
  </div>
 <% end %>
</div>

This partial is used twice in my dashboard/index.html.erb Once to render a collection of received submissions and again to render a collection of sent submissions:

dashboards/index.html.erb

<% if @received_submissions.length > 0 %>
 <div class="pure-u-1" id="projects"><h3 class="red">Final Tracks Received (<%= @received_submissions.length %>)</h3>
  <%= render :partial => :submission, :collection => @received_submissions %>
 </div>
<% end %>

<% if @sent_submissions.length != 0 %>
 <div class="pure-u-1" id="projects"><h3 class="red">Final Tracks Sent (<%= @sent_submissions.length %>)</h3>
  <%= render :partial => :submission, :collection => @sent_submissions %>
 </div>
<% end %>

However, rendering the received submissions works without ailment, and rendering the sent submissions errors out with:

undefined method `submissions' for #<PlayerProject:0x000001052730e8>

Here is the dashboards_controller also, just in case:

class DashboardsController < ApplicationController
 before_filter :authenticate_user!

 def index
  @compact = current_user.projects.where.not(status: ['Deleted','Closed']).order("id desc")
  @projects = current_user.projects.where.not(status: ['Deleted','Closed']).order("id desc")
  @received_messages = current_user.received_messages
  @received_submissions = current_user.projects.collect{|p| p.submissions }.flatten

  @player = current_user.player_projects.collect{|pp| pp.project}
  @sent_submissions = current_user.player_projects.collect{|ps| ps.submissions }.flatten
 end
end

models/player_project.rb'

class PlayerProject < ActiveRecord::Base
  belongs_to :project
  belongs_to :player, :class_name => "User"
end

models/submission.rb

class Submission < ActiveRecord::Base
 belongs_to :submitter, :class_name => "User"
 belongs_to :recipient, :class_name => "User"
 belongs_to :project

 validates :amount_in_cents, :presence => true, length: { minimum: 2, :message => " must be more than $0" }
 validates :description, :presence => { :message => " cannot be blank" }
 validates :final_track_url, :presence => { :message => " Required" }

 def price
     sprintf "%.2f", (self.amount_in_cents.to_f/100.0)
 end

 def price=(val)
     self.amount_in_cents = (val.to_f*100.0).to_i
 end

 def paid?
     !!self.paypal_confirmation
 end

end
2
  • Do you have the has_many :player_projects association in your User model? Commented May 28, 2014 at 6:02
  • Yes, I actually have has_many :player_projects, :foreign_key => 'player_id' Commented May 28, 2014 at 6:16

2 Answers 2

2

I think your error will be model-level:

undefined method `submissions' for #<PlayerProject:0x000001052730e8>

Association

This typically suggests you don't have a particular association set up (I.E you're calling @model.x when x is not defined):

@sent_submissions = current_user.player_projects.collect{|ps| ps.submissions }.flatten

Although your player_projects is defined, I think your submissions attribute / association does not exist. Besides, you can use the pluck method to help this: current_user.player_projects.pluck(:submissions)

Firstly, do you have the submissions attribute in your table?

Secondly, if you don't, what are you trying to achieve with it? You don't typically have a plural column in your table (denotes multiple data stores). I would use an association on a join-model

Sign up to request clarification or add additional context in comments.

13 Comments

I am not sure if I am providing the information that you need...but the submission has a column for submitter_id which is what I actually need to compare to the current user to distinguish if it was sent by them. So maybe it needs to be a where(submission.submitter.id = current.user.id") or something of that nature?
Okay, so you're trying to cycle through the submissions association on the player_projects model?
I tried @sent_submissions = current_user.player_projects.where(submitter_id: current_user) I am not sure if this is getting closer perhaps?
It will be getting closer for suer - what are you trying to achieve? Just show the submissions for a particular project, for a particular user?
btw your query needs to be: @sent_submissions = current_user.player_projects.where(submitter_id: current_user.id)
|
1

Try this

<%= render "submission", :collection => @sent_submissions %>

1 Comment

Thank you for the suggestion; It may help in the long run, at least to simplify my code, but I am still receiving the same error. Thank you still.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.