2011-07-08 75 views
0

上下文:我真的在用這個問題撓頭,我看過其他代碼來看看這是如何完成的。我很欣賞這是編碼的一個根本簡單的部分。說實話,到目前爲止,我可能會濫用類方法,但self.attrib = x不適用於類方法。無論如何,我的問題。找不到方法

我有這個在我的模型:

def self.get_user 
    @people = Person.where(:mp => nil) 
    @people.each do |person| 
     person.get_link(person.postcode) 
    end 
    end 

    def get_link(postcode) 
    base = "http://news.bbc.co.uk/democracylive/hi/search?q=" 
    postcode = postcode 
    target = "//a[starts-with(@class, 'name')] /@href" 
    url = base + postcode 
    page = Nokogiri::HTML(open(url)) 
    mps = [] 
    page.xpath(target).each do |node| 
     mps << node.text 
    end 
    link = mps[0] 
    self.get_email(link, postcode) 
    end 

現在person.get_link(person.postcode)部分拋出了一個沒有方法在控制檯中發現的錯誤:/我真的不明白爲什麼,它顯然存在。我能想到的唯一的事情是數據類型不正確 - 問題是我不知道如何糾正它。

真的很感謝任何指針。

(注:我知道這種方法可能不是它可以是最好的,我有點小白,但我到達那裏 - 緩慢但肯定:))

編輯:添加堆棧跟蹤

NoMethodError:未定義方法get_link' for #<Person:0x103633cc0> from /Library/Ruby/Gems/1.8/gems/activemodel-3.0.7/lib/active_model/attribute_methods.rb:367:in method_missing的 ' 從/Library/Ruby/Gems/1.8/gems/activerecord-3.0.7/lib/active_record/attribute_methods.rb:46:in method_missing' from /Users/geoff/RailsWork/mpmail/app/models/postcode.rb:8:in GET_USER' 從/庫/紅寶石/寶石/1.8/gems/activerecord-3.0.7/lib/active_record/relation.rb:13:in each' from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.7/lib/active_record/relation.rb:13:insend' 從/Library/Ruby/Gems/1.8/gems/activerecord-3.0.7/lib/active_record/relation.rb:13:in each' from /Users/geoff/RailsWork/mpmail/app/models/postcode.rb:7:in GET_USER」 從(IRB):2

+0

這些方法在什麼類? –

+0

你能提供一個堆棧跟蹤嗎? –

+0

@Mark Thomas - 上課是Postcode.rb –

回答

3

您所呼叫的get_link方法當你在Postcode類中定義它時,如果我正確地讀取了你的堆棧跟蹤的話。將get_link移至Person類並查看會發生什麼。

+0

Doh!完全錯過了。這就是爲什麼看到整個班級是有幫助的:) – Skilldrick