Ruby on rails 循环遍历特定数据库并获取ActiveRecord::Associations::CollectionProxy与每个
我想循环浏览特定用户的所有团队成员,并在特定页面上显示他们的所有答案,而不是显示特定用户的团队成员的所有答案。这是给我看的Ruby on rails 循环遍历特定数据库并获取ActiveRecord::Associations::CollectionProxy与每个,ruby-on-rails,rails-activerecord,Ruby On Rails,Rails Activerecord,我想循环浏览特定用户的所有团队成员,并在特定页面上显示他们的所有答案,而不是显示特定用户的团队成员的所有答案。这是给我看的 Updates Prashanth <ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Answer:0x00000001b2ce28> Sai <ActiveRecord::Associations::Collec
Updates
Prashanth
<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Answer:0x00000001b2ce28>
Sai
<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Answer:0x00000001b21230>
这是我的答案
class AnswersController < ApplicationController
before_action :authenticate_user!
def index
@answers = current_user.team_members.each do |team_member|
team_member.answers.all
end
end
end
要获取答案列表,我应该在index.html或asnwers控制器中更改什么?如果您的answer.answers是一个字符串数组,只需将视图行更改为:
<small><%= answer.answers.join ', ' %></small>
如果answer.answers包含包含包含实际答案字符串的字段的记录,则在该字段名为“body”时使用,否则将body更改为字段名:
<small><%= answer.answers.map(&:body) %></small>
在控制器中尝试以下操作:
class AnswersController < ApplicationController
before_action :authenticate_user!
def index
@answers = Answers.where(team_member: current_user.team_members)
end
end
在你看来:
<% if @answers.count > 0 %>
<% @answers.each do |answer| %>
<blockquote>
<p><%= answer.name %></p>
<small><%= answer.answer %></small>
</blockquote>
<% end %>
<% else %>
<p><em>Oops, doesn't look like there are any posts yet.</em></p>
<% end %>
张贴控制器代码。因此,我们可以看到@answers是从何而来添加的answers controller让我把事情弄清楚:您想列出属于您当前用户团队成员的所有答案吗?是的。对没有明确表示歉意。
class AnswersController < ApplicationController
before_action :authenticate_user!
def index
@answers = current_user.team_members.each do |team_member|
team_member.answers.all
end
end
end
<small><%= answer.answers.join ', ' %></small>
<small><%= answer.answers.map(&:body) %></small>
class AnswersController < ApplicationController
before_action :authenticate_user!
def index
@answers = Answers.where(team_member: current_user.team_members)
end
end
<% if @answers.count > 0 %>
<% @answers.each do |answer| %>
<blockquote>
<p><%= answer.name %></p>
<small><%= answer.answer %></small>
</blockquote>
<% end %>
<% else %>
<p><em>Oops, doesn't look like there are any posts yet.</em></p>
<% end %>