Rails find over multiple models
I think I'm missing something very obvious and its making my brain hurt.
class User < ActiveRecord::Base
has_one :profile
class Profile < ActiveRecord::Base
has_one :user
belongs_to :team
I have a partial that loops through the users and print some basic info, I'm using this partial in my team show page.
I had originally written this to return users who's profiles were a member of a 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
But quickly realized @profiles was an array, and it looks messy as hell. Stuck as to what my find should look like to select all User who have a profile that is a member of a team.
The partial that is working elsewhere for displaying users looks like this
<% for user in @users%>
<table>
<tr>
<td>
<%= image_tag user.profile.picture.url %>
开发者_如何转开发 </td>
<td>
<a href="/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 %>
Development log output with updated show and relationships
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]
I would change your associations to something like this:
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
I think the problem you are having is that in a has_one relationship one has to be the owner and one has to be the ownee.. (belongs_to)
Then you should be able to find associations using the following:
@user.profile
@team.users
@team.profiles
etc..
If you want the user to have a profile and the team to display its user's profiles you would change the models to this:
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
This way you could load your team, its users and the users profiles:
@team = Team.find(params[:id])
@users = @team.users
@profiles = @team.profiles
Good luck!
In order for your associations to work properly with the above code your db tables will need to have the following:
Your profiles table will need to have user_id as an integer.
Your users table will need to have team_id as an integer.
Your teams table doesn't need any ids.
Verify that your migrations setup these id's properly and that when a user is created that it is associated with your team properly:
@team.users << @user
Hope that helps!
Change your association as follows:
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
Make sure you have user_id
and team_id
columns on profiles
table.
Change your show
method as follows:
def show
@team = Team.find(params[:id])
@profile= @team.profile
@user = @profile.user
end
PS:
Above code will work if a team has one profile and a profile belongs to a team and a user.
精彩评论