Ruby on rails 显示多条相同记录的Rails

Ruby on rails 显示多条相同记录的Rails,ruby-on-rails,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby On Rails 3,Activerecord,我在下面提供我的活动记录。在view/users/show中,我想通过蓝图显示用户正在处理的任何项目。当用户向一个项目添加多个蓝图时,该项目会显示多次。我尝试了一些验证唯一性的选项,但没有结果 class Blueprint < ActiveRecord::Base attr_accessible :id, :name, :project_id, :user_id, :loc belongs_to :user belongs_to :project has_many :co

我在下面提供我的活动记录。在view/users/show中,我想通过蓝图显示用户正在处理的任何项目。当用户向一个项目添加多个蓝图时,该项目会显示多次。我尝试了一些验证唯一性的选项,但没有结果

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
classblueprint:蓝图
有很多评论
结束
类用户:蓝图
结束
以下是显示同一项目的多个值的视图代码

    <% @user.blueprints.each do |blueprint| %>
      <tr>
        <td><%= link_to blueprint.project.name, project_path(blueprint.project) %></td>
      </tr>
    <% end %>


谢谢

在用户的
项目
关系中,尝试将
uniq
选项设置为
true

class User < ActiveRecord::Base
  has_many :projects, :through => :blueprints, :uniq => true
end
class用户:blueprints,:uniq=>true
结束

既然用户中已经有了项目关联,为什么不循环浏览用户的项目而不是蓝图呢

<% @user.projects.each do |project| %>
      <tr>
        <td><%= link_to project.name, project_path(project) %></td>
      </tr>
    <% end %>


成功了!现在,我将花一些时间来研究为什么这会给我带来这么多麻烦:?您在循环浏览蓝图,因此,对于用户拥有的每个蓝图,都会生成一个链接,即使该链接是一次又一次相同的东西。当您编写
has_many:projects,through::blueprints
时,您告诉rails您只需要该用户的项目,为了弄清楚该用户有哪些项目,请查看blueprints表。希望这有助于澄清Sit所做的事情。我很好奇为什么我一开始就这么做。谢谢你的帮助。进一步说,我去修复另一端的同一个问题,并意识到在添加cgat的解决方案时,我意外地离开了您的修复。你们两个都帮了我的忙!