2012-12-15 2 views
0
<%= m.members %> 

이렇게하면 아래 배열이 반환됩니다. 이 배열의 마지막 행 (Ray의 레코드) 만 가져 오려면 어떻게 코딩 할 수 있습니까?Ruby on Rails에서 배열의 마지막 행을 가져올 수 있습니까?

[#<User id: 4, username: "John", age: 27, deleted_at: nil>, 
#<User id: 8, username: "Mike", age: 27, deleted_at: nil>, 
#<User id: 13, username: "Ray", age: 27, deleted_at: nil>] 

답변

1
[#<User id: 4, username: "John", age: 27, deleted_at: nil>, 
#<User id: 8, username: "Mike", age: 27, deleted_at: nil>, 
#<User id: 13, username: "Ray", age: 27, deleted_at: nil>].last 


우리는 다음과 같이 마지막과 첫 번째 요소를 얻고 싶다면 :
예 : 편곡 = [1,2,3,4]
$>arr.last을 => 4
$>arr.first => 1

+0

답장을 보내 주셔서 감사합니다 :) 내가 전에 그것을 시도하지만 <사용자를 다시 제공합니다은 0x00000 017d55298> :( – HUSTEN

+0

그 뒤에'.inspect'라고 쓰십시오. – VenkatK

+0

Array의 디폴트'to_s' 메쏘드는 Ruby 1.9의'inspect'와 같습니다. 개별 요소의 to_s 메소드는 다를 수 있습니다. 어쨌든 HTML에 원시 레코드를 덤프해서는 안되며 대신 특정 속성에 액세스해야합니다. 'to_yaml' 또는'to_json'은 종종 더 읽기 쉬운 출력을 제공합니다. – tadman

관련 문제