HUSTEN
HUSTEN

Reputation: 5197

How can I fetch last row of the array in Ruby on rails?

<%= m.members %>

This gives back this array below. If I want to get only last row of this array(Ray's record), how can I code?

[#<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>]

Upvotes: 0

Views: 222

Answers (1)

VenkatK
VenkatK

Reputation: 1305

[#<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


If we want to get the last and first elements as follows:
Ex: arr = [1,2,3,4]
$> arr.last => 4
$> arr.first => 1

Upvotes: 1

Related Questions