找不到方法

时间:2011-07-08 11:27:49

标签: ruby-on-rails ruby-on-rails-3

上下文:我真的很难理解这个问题,我已经看过其他代码,看看这是怎么做的。我很欣赏这是编码的一个根本简单的部分。老实说,到目前为止,我可能过度使用了类方法,但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)部分在控制台中抛出了一个no method found错误:/我字面上不明白为什么,它显然存在。我唯一能想到的是数据类型不正确 - 问题是我不知道如何纠正。

非常感谢任何指示。

(注意:我知道这种方法可能不是最好的,我有点像菜鸟,但我到了那里 - 慢慢地,但肯定:) :)

编辑:添加堆栈跟踪

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'     from /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'     来自/Library/Ruby/Gems/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:in 发送'     来自/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

1 个答案:

答案 0 :(得分:3)

如果我正在正确读取堆栈跟踪,那么在Postcode类中定义它时,您正在Person类上调用get_link方法。将get_link移动到Person-class,看看会发生什么。