# NoMethodError from ActiveRecord::Base#save

**URL:** <https://www.ruby-forum.com/t/nomethoderror-from-activerecord-base-save/143173>\
**Category:** Rails\
**Created:** [July 15, 2008, 11:54pm UTC](https://www.ruby-forum.com/t/nomethoderror-from-activerecord-base-save/143173 "2008-07-15T23:54:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Orbipedis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/cdc98d/32.png) [@Orbipedis](https://www.ruby-forum.com/u/Orbipedis)\
**Post date:** [July 15, 2008, 11:54pm UTC](https://www.ruby-forum.com/t/nomethoderror-from-activerecord-base-save/143173/1 "2008-07-15T23:54:17Z")

</div>

I have a written a simple RoR program that accepts a plaintext upload  
from a  
user and then emails that file capitalized to an email address aquired  
through a form. I am trying to utilize ActiveRecord to create a list of  
past  
upload , but I receive a NoMethodError whenever I try to save my model.  
The  
error follows:

`NoMethodError in UploadController#upload`

`
You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.delete
RAILS_ROOT: C:/RailsApps/SpecEd1
Application Trace:
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:124:in
rollback_active_record_state!' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:110:in save!’
app/controllers/upload_controller.rb:18:in `upload’
Request
Parameters:
{“Upload”=>“Upload,
Capitalize,
and Email”,
“emailaddress”=>“removed_email_address@domain.invalid”,
“datafile”=>#ActionController::UploadedStringIO:0x3442a34}
Response
Headers:
`

`{“cookie”=>[],
“Cache-Control”=>“no-cache”}`

And here is my Model:  
require ‘net/smtp’

class DataFile \< ActiveRecord::Base  
def initialize(incoming\_file)  
@file\_name = incoming\_file.original\_filename  
@content\_type = incoming\_file.content\_type  
@file = incoming\_file  
end

def uploaded\_file=(incoming\_file)  
@file\_name = incoming\_file.original\_filename  
@content\_type = incoming\_file.content\_type  
@file = incoming\_file  
end

def file\_name=(new\_file\_name)  
write\_attribute(“file\_name”, sanitize\_filename(new\_file\_name))  
end

def save\_file  
File.open(“#{RAILS\_ROOT}/public/uploaded/#{@file\_name}”, “wb”) do  
|f|  
f.write(@file.read)  
end  
end

def email(address, time)  
filedata = “”  
File.open(“#{RAILS\_ROOT}/public/uploaded/#{@file\_name}”, “r”) do  
|f|  
if @content\_type === ‘text/plain’  
lines = f.readlines  
Net::SMTP.start(‘smtp.psu.edu’, 25, ‘arl.psu.edu’,  
‘zkg5001’,  
‘4b4eebhjkasdfyu’, :plain) do |smtp|  
smtp.send\_message(generate\_email(lines, time),  
‘removed\_email\_address@domain.invalid’, address)  
end  
else  
Net::SMTP.start(‘smtp.psu.edu’, 25, ‘arl.psu.edu’,  
‘zkg5001’,  
‘4b4eebhjkasdfyu’, :plain) do |smtp|  
smtp.send\_message(“file was not plaintext”,  
‘removed\_email\_address@domain.invalid’, address)  
end  
end  
end  
end

def to\_s  
“File-- name:#{@file\_name} type:#{@content\_type}”  
end

private

def sanitize\_filename(file\_name)  
just\_filename = File.basename(file\_name)  
just\_filename.gsub(/[^\w.-]/,‘\_’)  
end

def generate\_email(lines, time)  
email = “Time of fileprocess is: File processed at:  
#{time.hour}:#{time.min}:#{time.sec}  
#{time.mon}/#{time.mday}/#{time.year}\n\n”  
email \<\< lines.join(‘\n’).upcase  
File.open(“C:\myLog.txt”, “w”) do |f|  
f.puts(email)  
end

```
  return email

```

end

end

And its migration:  
class DataFiles \< ActiveRecord::Migration  
def self.up  
create\_table :data\_files do |t|  
t.column :file\_name, :string  
t.column :content\_type, :string  
t.column :file, :text  
end

end

def self.down  
drop\_table :data\_files  
end  
end

and the controller:  
require ‘date’

class UploadController \< ApplicationController  
protect\_from\_forgery :only =\> [:create, :update, :destroy]

def index  
render :file =\> ‘app\views\upload\index.html.erb’  
end

def upload  
@myData = DataFile.new(params[‘datafile’])  
File.open(“C:/myLog.txt”, “w”) do |f|  
f.puts @myData  
end

```
  #@myData.uploaded_file = params['datafile']
  @myData.save_file
  @myData.save!
  timeOfUpload = DateTime.now

```

if(/^[A-Z0-9.\_%±]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i.match(params[‘emailaddress’]))  
@myData.email(params[‘emailaddress’], timeOfUpload)  
redirect\_to :action =\> ‘index’  
else  
redirect\_to :action =\> ‘invalid\_email’  
end  
end

def invalid\_email  
render :file =\> ‘app/views/upload/invalid\_email.html.erb’  
end

def history  
render :file =\>‘app/views/upload/history.html.erb’  
end

end

I don’t know why the exception is being thrown or how to fix it. Any  
assistance would be much appreciated

–  
View this message in context:  
[http://www.nabble.com/NoMethodError-from-ActiveRecord%3A%3ABase-save-tp18473677p18473677.html](http://www.nabble.com/NoMethodError-from-ActiveRecord%3A%3ABase-save-tp18473677p18473677.html)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).
