0

I'm a rails beginner and trying to put some code in the model. The code below is an illustration.

View:

Player_stats: <%= @player.player_pass_completion_ratio %>

Model:

class Player < ActiveRecord::Base
 has_many :lefthandstats
 has_many :righthandstats

def player_pass_completion_ratio
 Hands = [ lefthandstats, righthandstats] #These are objects & calling   @player.lefthandstats.find_with_passes directly generally works

 if self.category ==  "Hands"
  total_usual_passes = 500
  Hands.each do |cmethod|
    if self.cmethod.find_with_passes(:passes, :first, {:conditions => 'passes>200' })   then accuratestats += 1 end
  end
 end

accuracy = (accuratestats/total_usual_passes)*100
end

I get an undefined method "cmethod" when I try to call the code from the view. Any advice is greatly appreciated.

1
  • On the first line of the method, what this seems to be saying is that you want to place two local variables names lefthandstats and righthandstats in an array called Hands. But, since this is the first line in the method, these two variables havn't been defined so they are nil. Commented Feb 27, 2013 at 14:19

4 Answers 4

2

Comments in ruby use the # character, not //

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

1 Comment

while this is true, it doesn't answer the question.
1

Your code is calling self.cmethod, which will try to call the cmethod method on your object (which doesn't exist).

I believe what you're trying to do is something like the following:

hands = [:lefthandstats, :righthandstats]
hands.each do |cmethod|
  self.send(cmethod).... #rest of your code goes here
end

This will dynamically call the lefthandstats and righthandstats methods on your object.

Comments

1

Get rid of "self.cmethod" and just use "cmethod"

if cmethod.find_with_passes....

In the scope of the block "cmethod" is just a local variable. By putting self in front of it, ruby assumed you were calling a method on the containing class instance.

6 Comments

Thanks for your input. I took it off and now get undefined method /' for nil:NilClass`. Is it okay to pass methods in a block the way I have implemented?
the more I'm looking at it, the code as written can't work. You have two 'somethings' in an array called Hands. To have this work, the 'something' that your calling find_with_passes on must have that method defined. You say that lefthandstat is an object. What type of object?
BTW, the undedeinfed method nil:NilClass is because the objects in the "Hands" array are nil.
@player.lefthandstats.find_with_passes generally works and now I want to generalize it as a method I can call on a group of specific child objects of @player (lefthandstats, righthandsstats, leftfoot, rightfoot...)
Ok, I think that @sevenseacat's answer might be what you're looking for. By creating an array containing symbols :lefthandstats instead of variables lefthandstats you can then pass that symbol into a send method.
|
0

First correct your code by replacing // by # as in rails we use # to comment the code as mentioned by akofink.

Then consider this in context of your code:

@result = Player.all

@result.each do |player|

player.name

end

Here @result is returning a collection of players. So you can use a loop with @result.each such that for each result as player, you are getting a name of each player.

Correct your code with the knowledge of what is in the above.

Comments

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.