简体   繁体   English

我无法使Active Record简单查询

[英]I can't make Active Record simple query

This: 这个:

User.where 'id < ?', 5

fails with this message: 失败了这条消息:

NoMethodError: undefined method `join' for #<Class:0x007fa4c9b5a598>

When I try: 当我尝试:

User.where 'id < 5'

it works. 有用。

In another project: 在另一个项目中:

User.where 'id < ?', 5

works. 作品。

Where could be my problem? 哪里可能是我的问题?

Update 更新

I could solve the problem but commenting a gem: 我可以解决问题,但评论一个宝石:

# gem 'cancan'

Working flow 工作流程

Then when I run console, I get: 然后当我运行控制台时,我得到:

load error: ~/.irbrc
NameError: uninitialized constant ApplicationController::CanCan

And my simple query works. 我的简单查询有效。

Not working flow 不工作流程

Then I comment CanCan part of application controller and console starts without problems, but when I try my simple query it fails with the same error. 然后我评论CanCan部分应用程序控制器和控制台启动时没有问题,但是当我尝试我的简单查询时它失败并出现相同的错误。

What I am trying now 我现在在尝试什么

Searching for join in ~/.irbrc , I got several results. 搜索join~/.irbrc ,我有几个结果。 But I am not sure of what I am doing. 但我不确定我在做什么。

Where could be my problem? 哪里可能是我的问题?

Not really an answer to your question, but I personally try to use as little straight SQL as possible in my code. 不是你的问题的答案,但我个人尝试在我的代码中使用尽可能简单的SQL。

Check out the squeel gem, which will help with this, especially this kind of query. 查看squeel gem,这将有助于此,特别是这种查询。 https://github.com/ernie/squeel https://github.com/ernie/squeel

I had this snippet in my ~/.irbrc 我的~/.irbrc有这个片段

203   # instead of User.find(...) you can do user(...)
204   #   without arguments it only returns the model class
205   #   based on http://www.clarkware.com/blog/2007/09/03/console-shortcut
206 #  Dir.glob( File.join(Dir.pwd, *%w<app models ** *.rb>) ).map { |file_name|
207 #    table_name = File.basename(file_name).split('.')[0..-2].join

I commented all the block and now it works. 我对所有块进行了评论,现在它可以工作了。 Well, I copied that file and never have understood it. 好吧,我复制了那个文件,从来没有理解过它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM