从Rails中的关联表中选择和显示列

时间:2010-01-24 19:53:33

标签: ruby-on-rails join include find associations

我正在开发一个最终成为管理仪表板的框架。仪表板最终必须做的事情之一是允许用户搜索游戏的用户并查看有关他们的信息。有关他们的信息在不同的表格中,所以我自然不得不使用协会。现在,我只是想设置一个索引,以便在深入了解搜索和运行之前我知道协会的工作。

我正在尝试通过一个名为bets_users的表来加入一个名为users的表,其中包含一个名为bets的表。我有模型,控制器和索引视图用于下注,但是当我尝试将bets模型与用户模型组合时,我在Bets #index中获得NoMethodError。

投注模型:

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

用户模型:

class User < ActiveRecord::Base

has_many :bets_users
has_many :bets, :through => :bets_users

end

投注控制器

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

投注索引视图

<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>

我试图显示的字段以及来自bets表的数据是users.displayname。当我将displayname更改为id时,我不再收到错误,但显示的id不是来自任何表的数据。它是一个长约7位的长整数。

以下是错误:显示app / views / bets / index.html.erb,其中第18行引发:#

的未定义方法`displayname'

这是我日志文件中的sql:

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)) 

1 个答案:

答案 0 :(得分:3)

bet.usersArray(而不是User对象),这就是.displayname不起作用的原因。

你可以bet.users.each {|user| user. displayname }

你得到的id是ruby对象的id。