Topic: can't call associcerade table

can not call it associcerade table. I have a player who can have a result. but the results may have more players. what is my fault in the code

view:

http://pastebin.com/0rRbyM36

Controller :

http://pastebin.com/9YFLZVcw

Model:

http://pastebin.com/iEurQpFc

http://pastebin.com/gWAP1zFE

migrations:

http://pastebin.com/ABvxuWWv

http://pastebin.com/AguiuRYA

Last edited by Massen (2012-05-25 03:29:58)

Re: can't call associcerade table

You should put the association name in plural:

    class Result < ActiveRecord::Base
      attr_accessible :sum, :cal, :result
      has_many :players #that's where you had the error, you had 'player' in singular
    end

Re: can't call associcerade table

Hi!

I change player to players but i found nil still hmm

i can set in the view p.results_id.inspect but i can not set p.results.inspect :S

and when i SET p.results_id so find thid id number

Last edited by Massen (2012-05-25 05:43:55)

Re: can't call associcerade table

It is a little bit strange what you are doing in your ResultsController:

def index
    @playersname = Player.all
    @Resultname = Result.all
end

Why extracting all the players and all the results from the DB. What do you want to display in your results/index page?
Once you got all the players (not sure if it's what you need), you can loop on them and display a result the player linked to:

#ResultsController#index
def index
 @players = Player.all
end

#views/results/index.html.erb
<% @players.each do |player|%> 
<ul>
  <li><%= player.name %></li>        
        <li><%= player.result.sum%></li> #displaying just a sum as an example, but you can extract whatever you need from result object
</ul>
<%end%>

One more time, I don't know what you'd like to display, which are your business rules and logic. It's up to you yo adapt the code. See more info at Rails guides to have an idea how to link your models together. Hope this helps.

Last edited by Javix (2012-05-25 06:15:07)

Re: can't call associcerade table

what I want to show the players that are linked to their performance .. which I then will try to get into at random until one of my results, retrieved from the database.

Now came the problem up: undefined method `sum 'for nil: NilClass

there is not null!

Last edited by Massen (2012-05-25 06:32:11)

Re: can't call associcerade table

What you can do is to display a list of players with corresponding result values (sum, calc, etc.):

#in PlayersController
def index
  @players = Player.all
end

In views/players/index.html.erb:

<h1>Listing players</h1>

<table>
  <tr>
    <th>Name</th>
    <th>Results sum</th>    
    <th></th>
    <th></th>
  </tr>

<% @players.each do |post| %>
  <tr>
    <td><%= player.name %></td>    
    <td><%= player.result.sum %></td>   
    <td><%= link_to 'Edit', edit_player_path(player) %></td>
    <td><%= link_to 'Destroy', player, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>
<br />

<%= link_to 'New Player, new_player_path %>

You get nil when result's sum calling because that result object has 'sum' as NULL. Check it out.

Re: can't call associcerade table

but the "sum" is an attribute in my tablell "result". the "result" I have three attributes with the names. sum, cal, result. what I want to show the result / index is the player who is linking to a result. not edit, delete, etc..

Re: can't call associcerade table

if set .inspect on player.result.inspect so get Nil! but in my sqllite so have connectionen. hmm

Re: can't call associcerade table

It means that the indicated Player does NOT have an associated result. You can check it (result_id column in Players table).

Re: can't call associcerade table

it is they who are so konsigt that I get up a resultat_id but not if I should build on it by the sum, calc or nåogt else later

Re: can't call associcerade table

???? sorry I don't understand your English sad

Re: can't call associcerade table

oh, sry! i mean! they are the ones that are so strange that I get a resultat_id, but not if I should build on the sum, Calc or else later

Last edited by Massen (2012-05-28 08:17:44)

Re: can't call associcerade table

It is really weird, I get an ID number but after that I'll replace results_id to result.sum so says the nil?

Re: can't call associcerade table

Yes, my problem was i set Results i my migrations. this right was result