我通过Rails模型调用“未定义的方法”,但是通过Rails控制台可以正常调用

时间:2022-08-22 22:43:29

I receive undefined method scan for User:Class when scan method (line 13: last = resp.headers['Link'].scan(/\d+/).last) is called by Rails User model (inside class method self.create_user(auth)) :

我在扫描方法(第13行:last = resp.headers ['Link'] .scan(/ \ d + /..last)由Rails用户模型(内部类方法self.create_user)调用时收到User:Class的未定义方法扫描(auth)):

class User
  include Mongoid::Document

  field :email
  field :nickname

  embeds_many :posts

  def self.create_user(auth)

    conn = FaradayStack.build 'https://api.example.com'
    resp = conn.get "/users/#{auth.nickname}/list"
    last = resp.headers['Link'].scan(/\d+/).last # <== error occurs here
    n = 0

    create! do |user|
      user.email = auth["user_info"]["email"]
      user.nickname = auth['user_info']['nickname']

      while n <= last.to_i do
        resp = conn.get "/users/#{auth.nickname}/list?page=#{n=n+1}"
        resp.body.each do |repo|
          user.posts.build( html_url: "#{repo['html_url']}",
                                         description: "#{repo['description']}",
                                         created_at: "#{repo['created_at']}",
                                         pushed_at: "#{repo['pushed_at']}",
                                         avatar_url: "#{repo['owner']['avatar_url']}" )
        end
      end
    end
  end
end

If I call the same code by Rails console it works fine :

如果我通过Rails控制台调用相同的代码,它可以正常工作:

ruby-1.9.2-p136 :158 > last = resp.headers['Link'].scan(/\d+/).last
 => "16" 
ruby-1.9.2-p136 :159 > last = resp.headers['Link'].scan(/\d+/).last.to_i
 => 16 
ruby-1.9.2-p136 :160 > 

Something related to istance and class method concept I guess, but I can't fix it.

我想,与istance和类方法概念有关的东西,但我无法解决它。

UPDATE:

On suggestion of jimworm, I put rails logger instead of "offending" line :

关于jimworm的建议,我把rails logger而不是“冒犯”行:

Rails.logger.info "\r\n" + "#{Time.now} " + "resp.headers['Link']: #{resp.headers['Link']}" + "\r\n"

what I get is :

我得到的是:

2011-09-30 16:40:03 +0200 resp.headers['Link']: 
Redirected to http://localhost:3001/
Completed 302 Found in 3151ms
MONGODB blumb_dev['users'].find({:_id=>BSON::ObjectId('4e85d4c41d41c8103f000006')})

while in Rails console it is :

而在Rails控制台中它是:

ruby-1.9.2-p136 :181 > resp.headers['Link']
 => "<https://api.example.com/users/lgs/list?page=18>; rel=\"next\", <https://api.example.com/users/lgs/list?page=51>; rel=\"last\"" 
ruby-1.9.2-p136 :182 > 

Any idea ?

任何想法 ?

1 个解决方案

#1


3  

Our hero was perplexed... it worked in the console, surely it'll work from the controller? The village will be doomed if the bug wasn't caught. He decided to take a snapshot of the bug, catch it in the act.

我们的英雄很困惑......它在控制台中工作,当然它可以在控制器上运行吗?如果虫子没有被抓住,村庄将注定失败。他决定拍摄这个小虫的快照,然后抓住它。

Rails.logger.info...

The trap was set, then he waited. The screen flashed. A hit! The bug has left its traces in our trap. Our hero looked and...

设置了陷阱,然后他等了。屏幕闪烁。一击!这个bug在我们的陷阱中留下了痕迹。我们的英雄看起来......

to his surprise, the trap was empty!

令他惊讶的是,陷阱是空的!

2011-09-30 16:40:03 +0200 resp.headers['Link']: #nothing here#

"Now how could that be" he wondered, "a bug that left no traces? What kind of bug leaves no traces?"

“现在怎么可能”他想知道,“一个没有留下任何痕迹的错误?什么样的虫子没有痕迹?”

Then it came to him. "Eureka, a nil!" he smacked the table triumphantly, "a nil appears as nothing in the log! That's why I couldn't scan it!"

然后它来到他身边。 “尤里卡,没错!”他胜利地敲了敲桌子,“在原木中没有任何东西出现!这就是为什么我无法扫描它!”

The identity of the bug was revealed, and it wasn't long before our hero traced it back to its home at api.example.com and mashed it once and for all. Turns out it was the messages that the village was sending to api.example.com that lured the bug out of hiding. Now with the bug gone, the village was saved and everyone lived in peace, happily ever after.

臭虫的身份被揭示出来,不久之后我们的英雄将它追溯到api.example.com的家中并一劳永逸地捣碎它。事实证明,这是该村发送给api.example.com的消息,这些消息引诱了这个漏洞。现在随着虫子的消失,村庄得到了拯救,每个人都幸福地生活在一起。

#1


3  

Our hero was perplexed... it worked in the console, surely it'll work from the controller? The village will be doomed if the bug wasn't caught. He decided to take a snapshot of the bug, catch it in the act.

我们的英雄很困惑......它在控制台中工作,当然它可以在控制器上运行吗?如果虫子没有被抓住,村庄将注定失败。他决定拍摄这个小虫的快照,然后抓住它。

Rails.logger.info...

The trap was set, then he waited. The screen flashed. A hit! The bug has left its traces in our trap. Our hero looked and...

设置了陷阱,然后他等了。屏幕闪烁。一击!这个bug在我们的陷阱中留下了痕迹。我们的英雄看起来......

to his surprise, the trap was empty!

令他惊讶的是,陷阱是空的!

2011-09-30 16:40:03 +0200 resp.headers['Link']: #nothing here#

"Now how could that be" he wondered, "a bug that left no traces? What kind of bug leaves no traces?"

“现在怎么可能”他想知道,“一个没有留下任何痕迹的错误?什么样的虫子没有痕迹?”

Then it came to him. "Eureka, a nil!" he smacked the table triumphantly, "a nil appears as nothing in the log! That's why I couldn't scan it!"

然后它来到他身边。 “尤里卡,没错!”他胜利地敲了敲桌子,“在原木中没有任何东西出现!这就是为什么我无法扫描它!”

The identity of the bug was revealed, and it wasn't long before our hero traced it back to its home at api.example.com and mashed it once and for all. Turns out it was the messages that the village was sending to api.example.com that lured the bug out of hiding. Now with the bug gone, the village was saved and everyone lived in peace, happily ever after.

臭虫的身份被揭示出来,不久之后我们的英雄将它追溯到api.example.com的家中并一劳永逸地捣碎它。事实证明,这是该村发送给api.example.com的消息,这些消息引诱了这个漏洞。现在随着虫子的消失,村庄得到了拯救,每个人都幸福地生活在一起。