2010-03-30 54 views
0

我想我錯過了一些非常明顯的東西,它讓我的大腦受傷。Rails在多個模型中查找

class User < ActiveRecord::Base 
has_one :profile 

class Profile < ActiveRecord::Base 
has_one :user 
belongs_to :team 

我有一個局部循環遍歷用戶並打印一些基本信息,我在我的團隊顯示頁面中使用了這個部分。

我原本寫這個是爲了返回用戶的個人資料是團隊成員。

def show 
@team = Team.find_by_id(params[:id]) 
@profiles= Profile.find(:all, :conditions => ['team_id = ?', @team.id]) 
@users = User.find_by_id(@profiles.user_id) 
end 

但很快意識到@profiles是一個數組,它看起來很亂。卡住我的發現應該看起來像選擇所有擁有屬於團隊成員的個人資料的用戶。

一個用來顯示用戶一起工作的其他地方看起來像一個更新的節目和關係這

<% for user in @users%> 
<table> 
<tr> 
     <td> 
     <%= image_tag user.profile.picture.url %> 
     </td> 
     <td> 
     <a href="https://stackoverflow.com/users/<%= user.id %>"><%= user.login %></a> 
     </td> 
     <td> 
     <%= user.profile.first_name %> <%= user.profile.second_name %> 
     </td> 
     <td> 
     <%= user.profile.status %> 
     </td> 
</tr> 
</table> 
<% end %> 

開發日誌輸出

Processing TeamsController#show (for 127.0.0.1 at 2010-03-30 22:06:31) [GET] 
    Parameters: {"id"=>"1"} 
    User Load (1.3ms) SELECT * FROM "users" WHERE ("users"."id" = 3) LIMIT 1 
    Team Load (1.0ms) SELECT * FROM "teams" WHERE ("teams"."id" = 1) 
Rendering template within layouts/main 
Rendering teams/show 
Completed in 75ms (View: 11, DB: 2) | 200 OK [http://localhost/teams/1] 

回答

4

我會改變你的協會是這樣的部分:

class User < ActiveRecord::Base 
has_one :profile 

class Profile < ActiveRecord::Base 
belongs_to :user 
belongs_to :team 

class Team < ActiveRecord::Base 
has_many :profiles 
has_many :users, :through => :profiles 

我認爲你遇到的問題是在哈哈s_one關係一個必須是所有者和一個有可能成爲ownee ..(belongs_to的)

那麼你應該可以使用下面找到協會:

@user.profile 
@team.users 
@team.profiles 

等。

如果你想讓用戶有一個輪廓和團隊,以顯示其用戶的個人資料,你會模型改成這樣:

class User < ActiveRecord::Base 
has_one :profile 
belongs_to :team 

class Profile < ActiveRecord::Base 
belongs_to :user 

class Team < ActiveRecord::Base 
has_many :users 
has_many :profiles, :through => :users 

這種方式,你可以加載你的團隊,它的用戶和用戶配置文件:

@team = Team.find(params[:id]) 
@users = @team.users 
@profiles = @team.profiles 

祝你好運!

爲了使您的協會與上面的代碼你的數據庫表需要有以下正常工作:

你的個人資料表就需要有user_id說明爲整數。

您的用戶表需要將team_id作爲整數。

您的團隊表不需要任何ID。

驗證您的遷移設置這些ID的正確,當創建一個用戶,它是與您的團隊正確關聯是:

@team.users << @user 

希望幫助!

+0

它似乎沒有正確選擇用戶,雖然它在我在紙上工作時似乎很有意義。添加日誌和部分 – Kelvin 2010-03-30 21:07:34

+0

克格勃確保您的所有關聯設置正確?只要您的用戶與您的團隊相關聯,它就應該可以工作。我將添加您的表結構應該看起來像。 – 2010-03-30 21:11:29

+0

糟糕我也忘了在User模型下添加belongs_to聲明。這也可能會造成麻煩。 :)發佈編輯來反映。 – 2010-03-30 21:23:44

1

如下更改您的關聯關係:

class User < ActiveRecord::Base 
    has_one :profile 
end 

class Profile < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :team 
end 

class Team < ActiveRecord::Base 
    has_many :profiles 
    has_many :users, :through => :profiles 
end 

確保您有profilesuser_idteam_id列。

如下更改show方法:

def show 
    @team = Team.find(params[:id]) 
    @profile= @team.profile 
    @user = @profile.user 
end 

PS:如果一支球隊有一個配置文件和配置文件屬於團隊和用戶

上面的代碼將正常工作。

+0

我希望如何工作,它是一個用戶有一個配置文件,並且一個團隊有許多配置文件。在特定團隊的展示頁面上,我將列出所有用戶的個人資料。 – Kelvin 2010-03-30 19:36:05

+0

編輯我的答案爲您的目的kgb。乾杯! – 2010-03-30 19:56:54