Topic: Class scope question

I hope this the right place. My question is more ruby than rails. When I am writing models sometimes it is nessisary for me to use self and other times I don't need to. I was hoping some one could enlighten me on the subtleties that I am not seeing. They seem the same to me. Let me give you an example.This is a method in one of my models

def fetch_articles
    current_feed = get_rss_feed( 5 ) 
    debugger
    if current_feed.class == Feedzirra::Parser::RSS
      self.status = true
    else
      self.status = false
    end 
    self.save
    if status
      current_feed.entries.each do |article|
          unless Article.array_of_article_urls.include?(article.url)
            a = Article.new(title: article.title, content: article.content, source: article.url, clicks: 0)
            a.title ||= article.summary
            a.content ||= article.summary
            a.save
          end 
      end 
    end 
  end

Before I didn't have those self's it wouldn't persist the changes in the database then when I added them it worked.yet a little later this one works fine without self.

def display_status
    if status
      "<span class='label label-success'>Success</span>"
    else
      "<span class='label label-important'>Fail</span>"
    end 
  end 

Last edited by tyger86 (2012-10-03 17:30:40)

Re: Class scope question

Check this out http://jimmycuadra.com/posts/self-in-ruby

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Class scope question

Awesome thank you. So the call to status = created a local object that never got used but just calling status since it wasn't overwritten by a local assignment actually called obj.status. Great article thank you.