0
@connection = ActiveRecord::Base.establish_connection(
            :adapter => "mysql2",
            :host => "localhost",
            :database => "siteconfig_development",
            :username => "root",
            :password => "root123"
)

sql = 'SELECT * from users'

@success_data = @connection.connection.execute(sql);

如何在不迭代的情况下从@success_data 获取最后一条记录?

4

3 回答 3

1

更新

  @success_data = @connection.connection.select_all(sql);
  @success_data.class  
  => Array

你可以使用任何数组方法@success_data

  @success_data.last  # will return the last element   

或者

  @success_data[-1]   # will return the last element
于 2013-03-14T13:34:45.963 回答
1

sql = 'SELECT * from users order by id desc limit 1'

于 2013-03-14T13:29:12.713 回答
0

好吧,您可以.last对集合进行操作,但是您在这里所做的事情有点偏离轨道,这是您访问数据库的默认方式还是特殊情况?

于 2013-03-14T13:27:04.470 回答