我有这个疑问:Rails视图问题与ActiveRecord的关系
<% @answer = Answer.where(:question_id => @question.id, :correct => 1) %>
结果是:
#<ActiveRecord::Relation [#<Answer id: 535, body: "d", notice: "", correct: true, question_id: 50, created_at: "2015-09-26 10:09:10", updated_at: "2015-09-26 10:09:10">]>
当我想这样做:
<%= @answer.body %>
我得到这个错误:
undefined method `body' for <Answer::ActiveRecord_Relation:0x0000000e99b7c8>
怎么回事?
谢谢。我试着用.first。 这种情况下的区别是什么:第一个dosn't工作? – Felix
如果你没有收到'@ answer.body'的响应,这应该没有什么区别,这可能意味着':body'属性是空的。试试'rails console'中的'where'查询,这样你就可以看到完整的响应。 – miler350
神秘... ...第一次没有工作... – Felix