@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获取最后一条记录而不进行迭代?
答案 0 :(得分:1)
sql ='SELECT * from users order by id desc limit 1'
答案 1 :(得分: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
答案 2 :(得分:0)
你可以对集合做.last
但是你在这里做的有点偏离轨道路径,这是你访问数据库的默认方式还是一个特例?