2

我是 RoR 的新手,

环境:安装在 leopard 上的 Rails 3.09 和 ruby​​ 1.9.2p290

我正在仔细阅读 Michael hartl (http://ruby.railstutorial.org/) 的教程。在第 7 章,我们应该使用控制台测试这段代码:

$ rails console --sandbox
>> User.create!(:nom => "Michael Hartl", :email => "mhartl@example.com",
?>              :password => "foobar", :password_confirmation => "foobar")
>> user = User.find_by_email("mhartl@example.com")
>> user.has_password?("foobar")
=> true 

我的问题是使用基于属性的查找器 find_by_(属性的名称)。在我的示例中 find_by_email。

实际上,rails 似乎不接受 find_by_ 作为一种方法。仍然是上面的示例,在行 user.has_password?("foobar") rails 返回错误:NoMethodError: undefined method。方法 has_password? 在我的模型中定义良好。

我已经检查了 rails 3.0.9 的 api 的文档,如果我使用这种方式:

>> user = User.where(:email => "mhartl@example.com")

然后它工作....

所以我想知道为什么我不能使用方法 find_by_ ?其他查找方法例如:find.first、find.last 等。
谢谢你帮助我 fabien

4

1 回答 1

0

实际上,查找器在模型文件中工作,而不是在控制台(开发环境)中工作。奇异的法比恩

于 2011-08-30T16:24:22.217 回答