나는 다음과 같은 모델을 가지고 :Ruby Associations CollectionProxy에서 특정 필드를 가져 오는 방법은 무엇입니까?
class User < ApplicationRecord
has_many :hobbies, dependent: :destroy
end
class Hobby < ApplicationRecord
belongs_to :user
end
내보기 사용자와 취미에 출력하려고했다 :
<table>
<tr>
<th>User</th>
<th>Hobby</th>
</tr>
<% @users.each do |u| %>
<tr>
<td><%= u.name %></td>
<td><%= u.hobbies %></td>
</tr>
<% end %>
</table>
하지만 다음을 참조 :
User Hobby
Mad Max <Hobby::ActiveRecord_Associations_CollectionProxy:0x007fa2721ab910>
그것을 나는 컬렉션을 출력하고있는 것처럼 보이지만 실제 취미 이름을 원합니다. 나는 u.hobbies.first, u.hobbies [: 취미] 등을 시도했다. 나는 http://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html 내부를 들여다 보았지만 새로운 개발자로서 나에게 혼란 스럽다. 누군가가 컬렉션에서 특정 필드를 가져 오는 방법을 말해 줄 수 있습니까? 그리고 거기에 어떤 자료가 나에게 미래에 참조 할 좋은 지침입니까?