当我尝试对聚合使用列别名时出现未定义的方法错误 (PostgreSQL)
在我的模型中:
class B2bLoginAttempt < ActiveRecord::Base
set_table_name "b2b_logins"
end
在我的 Controller 中:
@client_ip = request.env['REMOTE_ADDR']
@sql = "select count(id) as failed_logins FROM b2b_logins WHERE ip_address = '"+@client_ip+"'"
f = B2bLoginAttempt.find_by_sql(@sql)
failed_attempts = f.failed_logins.to_s
f.destroy
然后我看到:#
最佳答案
出现错误是因为find_by_sql
返回的是一个数组,所以需要写f.first.failed_logins.to_s
而不是f.failed_logins.to_s
。
检查 find_by_sql
文档 here .
关于ruby-on-rails - 在 Rails 中使用数据库列别名时出现 NoMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13976273/