Reputation: 1567
I am providing my active records below. In view/users/show I want to display any project the user is working on, through blueprints. When a user adds multiple blueprints to a project, the project is showing up multiple times. I tried some validate_uniqueness options to no avail.
class Blueprint < ActiveRecord::Base
attr_accessible :id, :name, :project_id, :user_id, :loc
belongs_to :user
belongs_to :project
has_many :comments
end
class Project < ActiveRecord::Base
attr_accessible :id, :name
has_many :blueprints
has_many :users, :through => :blueprints
has_many :comments
end
class User < ActiveRecord::Base
attr_accessible :id, :name
has_many :blueprints
has_many :projects, :through => :blueprints
end
Here is the view code that is displaying multiple values of the same project.
<% @user.blueprints.each do |blueprint| %>
<tr>
<td><%= link_to blueprint.project.name, project_path(blueprint.project) %></td>
</tr>
<% end %>
Thanks!
Upvotes: 1
Views: 83
Reputation: 3909
Since you already have the projects association in the User, why don't you loop through the user's projects instead of the blueprints.
<% @user.projects.each do |project| %>
<tr>
<td><%= link_to project.name, project_path(project) %></td>
</tr>
<% end %>
Upvotes: 1
Reputation: 6213
Try setting uniq
option to true
in user's projects
relation like this
class User < ActiveRecord::Base
has_many :projects, :through => :blueprints, :uniq => true
end
Upvotes: 2