开发者

I get "undefined method" calling by Rails model, but works fine calling by Rails console

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)) :

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+开发者_如何学JAVA1}"
        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 :

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.

UPDATE:

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

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 :

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 ?


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...

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜