Selecting and Displaying Columns from Associated Tables in Rails
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]
Bet Load (4600.9ms)
SELECT `bets`.* FROM `bets` INNER JOIN `bets_users` ON (`bets`.`id` = `bets_users`.`bet_id`) INNER JOIN `users` ON (`users`.`id` = `bets_users`.`user_id`)
Bet Columns (97.4ms) SHOW FIELDS FROM `bets`
BetsUser Load (423.6ms) SELECT `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))
BetsUser Columns (95.6ms) SHOW FIELDS FROM `bets_users`
User Columns (1063.6ms) SHOW FIELDS FROM `users`
User Load (7109.4ms) SELECT * FROM `users` WHERE (`users`.`id` IN (726,1,2,4))
bet.users
is an Array
(and not a User
object) that's why the .displayname
doesn't work.
You could do bet.users.each {|user| user. displayname }
The id you get is the id of the ruby object.
精彩评论