2010-03-30 10 views
0

매우 분명한 뭔가를 잃어 버렸고 두뇌가 상처를 입었습니다.여러 모델에서 레일 찾기

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는 빠르게 배열로되어있어 지옥처럼 보입니다. 팀의 구성원 인 프로필이있는 모든 사용자를 선택하려면 내 찾기가 어떻게 보이는지 파악해야합니다.

표시하는 사용자를 위해 다른 곳에서 노력하는 내가 이런 식으로 연결을 바꿀 것

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

업데이트 쇼 및 관계이

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

개발 로그 출력처럼 보이는 부분 :

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 한 필요가있을 것이다 : 당신의 협회는 귀하의 DB 테이블이 다음 있어야합니다 위의 코드에서 제대로 작동하기 위해서는

.

사용자 테이블의 팀 ID는 정수 여야합니다.

팀 테이블에 ID가 필요하지 않습니다.

가 있는지 확인하여 마이그레이션 설정이 아이디의 적절하고 사용자가 제대로 팀과 연결되어 있는지를 만들 때 그 : 도움이

@team.users << @user 

희망!다음과 같이

+0

사용자가 올바르게 선택되지 않은 것 같습니다. 종이로 작업했을 때 제대로 된 것 같습니다. 추가 된 로그 및 부분적으로 – Kelvin

+0

KGB 모든 연결이 올바르게 설정되어 있는지 확인하십시오. 사용자가 팀과 연관되어 있으면 작업해야합니다. 표 구조가 어떻게 생겼는지 추가하겠습니다. –

+0

죄송합니다. 사용자 모델에 belongs_to 선언을 추가하는 것을 잊었습니다. 그것은 또한 문제를 일으킬 수 있습니다. :) 반영하도록 편집 된 게시물. –

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 

당신이 profiles 테이블에 user_idteam_id 열이 있는지 확인합니다. 다음과 같이

show 방법을 변경

:

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

PS : 팀이 하나 개의 프로파일을 가지고 있으며, 프로파일이 팀과 사용자에 속하는 경우 코드 위

가 작동합니다.

+0

나는 그것이 사용자가 프로필을 가지고 있으며, 팀이 많은 프로필을 가지고 있다는 것을 기대하고 있습니다. 특정 팀의 쇼 페이지에서 해당 팀의 프로필이있는 모든 사용자를 나열합니다. – Kelvin

+0

당신의 목적에 맞는 대답을 편집했습니다. 건배! –

관련 문제