上下文:我真的被这个代码弄得头晕目眩,我看了看其他代码,看看它是如何完成的。我理解这是编码的一个基本的简单部分。老实说,到目前为止,我可能已经过度使用了类方法,但是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)部分在控制台中抛出一个没有找到错误的方法:/我真的不明白为什么,显然是在那里。我唯一能想到的就是数据类型是不正确的--问题是我不知道如何纠正它。
真的很感谢你的指点。
(注意:我知道这种方法可能不是最好的方法,我是个菜鸟,但我正在逐步实现-缓慢但肯定:)
编辑:添加堆栈跟踪
/Library/Ruby/Gems/1.8/gems/activerecord-3.0.7/lib/active_record/attribute_methods.rb:46:in :未定义的方法get_link' for #<Person:0x103633cc0> from /Library/Ruby/Gems/1.8/gems/activemodel-3.0.7/lib/active_model/attribute_methods.rb:367:inmethod_missing‘来自get_link' for #<Person:0x103633cc0> from /Library/Ruby/Gems/1.8/gems/activemodel-3.0.7/lib/active_model/attribute_methods.rb:367:inmethod_missing' from /Users/geoff/RailsWork/mpmail/app/models/postcode.rb:8:inget_user,来自/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:inget_user‘from (irb):2
发布于 2011-07-08 11:42:39
如果我正确地读取您的堆栈跟踪,则在Postcode类中定义它时,您将调用Person类上的get_link方法。将get_link移动到Person类,看看会发生什么。
https://stackoverflow.com/questions/6623764
复制相似问题