【发布时间】:2012-03-01 05:43:13
【问题描述】:
我正在尝试弄清楚如何在我的一个模型中进行表连接。
有积分、有问题、有用户。
point.rb
class Point < ActiveRecord::Base
belongs_to :user
belongs_to :question
end
question.rb
class Question < ActiveRecord::Base
has_many :points
end
user.rb
class User < ActiveRecord::Base
在我的积分控制器中,我正在这样做:
def index
@points = Point.all
@user_points = Point.where('user_id' => current_user)
end
在我的points/index视图中:
<% @user_points.each do |user_point| %>
<tr>
<td><%= current_user.name %></td>
<td><%= user_point.question_id %></td>
<td><%= user_point.correct_answer %></td>
<td><%= user_point.user_answer %></td>
</tr>
<% end %>
我需要访问问题表中每个问题的名称(我的视图中有问题 ID。我是 Rails 的 n00b,无法通过文档弄清楚如何做到这一点。
【问题讨论】:
标签: ruby-on-rails join html-table arel