I am working on developing a skeleton of what to eventually become an administration dashboard. One of the things the dashboard will eventually have to do is allow users to search for users of a game and view information about them. The information about them in question is in different table so naturally I have had to employ associations. For now, I am just trying to get an index set up so that I know the associations work before diving into getting search up and running.
I am trying to join a table called users with a table called bets through a table called bets_users. I have the model, controller, and index view working for the bets, however when I try to combine the bets model with the users model I am getting a NoMethodError in Bets#index.
Bet Model:
class Bet < ActiveRecord::Base
has_many :bets_bet_odds
has_many :bet_odds, :through => :bets_bet_odds
has_many :bets_users
has_many :users, :through => :bets_users
def self.index_bets
find(:all, :joins => :users, :include => [:bets_users, :users])
end
end
BetsUser Model
class BetsUser < ActiveRecord::Base
belongs_to :bet
belongs_to :user
end
User Model:
class User < ActiveRecord::Base
has_many :bets_users
has_many :bets, :through => :bets_users
end
Bets Controller
class BetsController < ApplicationController
def index
@index_bets = Bet.index_bets
end
def edit
@bet = Bet.find(params[:id])
end
def update
@bet = Bet.find(params[:id])
if @bet.update_attributes(params[:bet])
redirect_to :controller => "bets", :action => "index"
end
end
end
Bets Index View
<h1>Bets#index</h1>
<p>Find me in app/views/bets/index.html.erb</p>
<table border = "1">
<tr>
<th>id</th>
<th>user</th>
<th>scored</th>
<th>updated_at</th>
</tr>
<% @index_bets.each do |bet| %>
<tr>
<td><%= link_to h(bet.id), :controller => "bets",
:action => "edit",
:id => bet.id %>
</td>
<td><%=h bet.users.displayname %></td>
<td><%=h bet.scored %></td>
<td><%=h bet.updated_at %></td>
</tr>
<% end %>
</table>
The field I am trying to display in addition the data from the bets table is users.displayname. When I change displayname to id I no longer get an error, however the id that is displayed is not data from any of the tables. It is a long integer about 7 digits long.
Here is the error: Showing app/views/bets/index.html.erb where line #18 raised: undefined method `displayname' for #
Here is the sql from my log file:
Processing BetsController#index (for 127.0.0.1 at 2010-01-24 14:48:22) [GET]
[4;36;1mBet Load (4600.9ms) [0m
[0;1mSELECT bets
.* FROM bets
INNER JOIN bets_users
ON (bets
.id
= bets_users
.bet_id
) INNER JOIN users
ON (users
.id
= bets_users
.user_id
) [0m
[4;35;1mBet Columns (97.4ms)[0m [0mSHOW FIELDS FROM bets
[0m
[4;36;1mBetsUser Load (423.6ms)[0m [0;1mSELECT bets_users
.* FROM bets_users
WHERE (bets_users
.bet_id IN (3,4,5,12,13,14,15,16,17,18,19,21,24,25,27,28,29,31,33,34,35,42,43,44,45,50,51,52,54,61,70,71,72,73,74,76,77,80,81,82,87,92,93,94,95,96,97,98,100,101,102,103,110,112,113,114,116,120,121,122,123,125,127,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,148,149,151,152,153,154,155,156,157,158,159,160,161,162,163,164,166,167,168,169,170,171,172,173,174,178,179,180,181,182,183,186,187,188,189,190,191,192,193,196,197,198,199,200,201,202,203,204,205,206,207,211,212,213,214,215,216,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,384,403,410,411,412,417,431,432,433,439,440,441,466,470,471,472,473,474,475,479,480,481,482,483,484,487,488,489,490,491,492,493,494,495,496,497,498,499,500,501,502,503,504,505,506,515,516,517,518,519,520,521,522,523,524,525,526,527,528,529,530)) [0m
[4;35;1mBetsUser Columns (95.6ms)[0m [0mSHOW FIELDS FROM bets_users
[0m
[4;36;1mUser Columns (1063.6ms)[0m [0;1mSHOW FIELDS FROM users
[0m
[4;35;1mUser Load (7109.4ms)[0m [0mSELECT * FROM users
WHERE (users
.id
IN (726,1,2,4)) [0m