Geoff Wright
Geoff Wright

Reputation: 125

Method not found

Context: I am really scratching my head with this one, I've had a look at other code to see how this is done. I appreciate this is a fundamentally simple part of coding. To be honest up until now I've probably overused class methods, but self.attrib = x doesn't work with class methods. Anyway, my problem.

I have this in my model:

  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

Now the person.get_link(person.postcode) part throws up a no method found error in the console :/ I literally don't understand why, its clearly there. The only thing I can think of is that the data type is incorrect - problem is I don't know how to correct that.

Really appreciate any pointers.

(Note: I know that method might not be the best it can be, I'm a bit of a noob but am getting there - slowly but surely :) )

EDIT: ADDED STACK TRACE

NoMethodError: undefined method 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 /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:inget_user' from /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:insend' from /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

Upvotes: 0

Views: 3171

Answers (1)

Maran
Maran

Reputation: 2736

You are calling the get_link method on the Person class while you defined it in the Postcode class, if I'm reading your stacktrace correctly. Move get_link to the Person-class and see what happens.

Upvotes: 4

Related Questions