So I think I’m close to getting the right syntax on the method below, but I’m not quite these. I always get confused as to when to use the “self” method. Here’s the context: the method below is meant to compare a file saved to the database against a file currently on a website. The method itself is a member of a model class in a rails app.
But when I run the code in the console, it gets stuck on the “self.” It can’t call the versions method on a nil class. What am I misunderstanding about the “self”? What do I need to do to make this method work?
def compare
live_file = download_file
archive_file = self.versions.last.changeset[:robots_file]
if live_file != archive_file
return mismatch
end
end
Edit: Here’s the error message I get in console.
NoMethodError: undefined method `versions' for nil:NilClass
from /Users/bendowney/sites/WatchApp/app/models/site.rb:14:in `compare'
from (irb):50
from /Users/bendowney/.rvm/gems/ruby-1.9.3-p194@WatchApp/gems/railties-3.2.5/lib/rails/commands/console.rb:47:in `start'
from /Users/bendowney/.rvm/gems/ruby-1.9.3-p194@WatchApp/gems/railties-3.2.5/lib/rails/commands/console.rb:8:in `start'
from /Users/bendowney/.rvm/gems/ruby-1.9.3-p194@WatchApp/gems/railties-3.2.5/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
EDIT: Answer is below.
You all ask the right questions. They got me thinking. I made two changes to my original method and now it works. I needed to save the file I downloaded (line 2 below) and I had a forgot to encapsulate my “mismatch” status in quotation marks. Thanks for your help!