我是 RoR 新手。我正在尝试查看我的 MySQL 连接是否设置正确。 我的 Controller 中有以下代码:
require 'rubygems' # Only if installed via RubyGems
class HomeController < ApplicationController
User.find(1)
def index
end
end
这在我的 user.rb 类中:
class User < ActiveRecord::Base
end
显然我在 MySQL 中有一个用户表。
我收到这样的错误:
undefined method `eq' for nil:NilClass
知道为什么吗?另外,如何将查询代码从 Controller 中取出并放入模型类型的文件中?
最佳答案
我认为你的代码应该看起来更像这样:
class HomeController < ApplicationController
def index
@user = User.find(1)
end
end
当使用基本查找等时,您应该在 Controller - ofc 中查询您的模型。
在更复杂的情况下,您应该使用范围: http://asciicasts.com/episodes/202-active-record-queries-in-rails-3
关于mysql - 通过ActiveRecord查询MySQL错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5863475/