# File upload help

**URL:** <https://www.ruby-forum.com/t/file-upload-help/194741>\
**Category:** Ruby\
**Created:** [September 9, 2010, 7:53pm UTC](https://www.ruby-forum.com/t/file-upload-help/194741 "2010-09-09T19:53:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@Shef](https://www.ruby-forum.com/u/Shef)\
**Post date:** [September 9, 2010, 7:53pm UTC](https://www.ruby-forum.com/t/file-upload-help/194741/1 "2010-09-09T19:53:12Z")

</div>

I’m fairly new to rails and I’m trying to understand an error it’s  
giving me. I’m trying to upload some text files to a rails app of mine  
and I keep getting a SystemStackError error(stack too deep), but what  
I’m trying to do seems simple enough.

So here’s the code from the model:

```
    def title=(doc_field)
            self.title = doc_field.to_s
    end

    def article=(doc_field)
            self.body = doc_field.to_s
    end

    def originals=(doc_field)
            self.originals = doc_field.to_s
    end

    def corrections=(doc_field)
            self.corrections = doc_field.to_s
    end

```

and the error rails issues:

/mnt/transfer/apps/rails/blog\_poster/app/models/config\_docs.rb:16:in  
`corrections=' /mnt/transfer/apps/rails/blog_poster/app/models/config_docs.rb:16:in`corrections=’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active\_record/  
base.rb:2906:in `send' /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/ base.rb:2906:in`assign\_attributes’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active\_record/  
base.rb:2902:in `each' /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/ base.rb:2902:in`assign\_attributes’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active\_record/  
base.rb:2775:in `attributes=' /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/ base.rb:2473:in`initialize’  
/mnt/transfer/apps/rails/blog\_poster/app/controllers/  
article\_controller.rb:13:in `new' /mnt/transfer/apps/rails/blog_poster/app/controllers/ article_controller.rb:13:in`upload’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/  
action\_controller/base.rb:1331:in `send' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/ action_controller/base.rb:1331:in`perform\_action\_without\_filters’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/  
action\_controller/filters.rb:617:in `call_filters' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/ action_controller/filters.rb:610:in`perform\_action\_without\_benchmark’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/  
action\_controller/benchmarking.rb:68:in  
`perform_action_without_rescue' /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/ active_support/core_ext/benchmark.rb:17:in`ms’  
/usr/local/lib/ruby/1.8/benchmark.rb:308:in `realtime' /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/ active_support/core_ext/benchmark.rb:17:in`ms’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/  
action\_controller/benchmarking.rb:68:in  
`perform_action_without_rescue' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/ action_controller/rescue.rb:160:in`perform\_action\_without\_flash’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/  
action\_controller/flash.rb:151:in `perform_action' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/ action_controller/base.rb:532:in`send’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/  
action\_controller/base.rb:532:in `process_without_filters' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/ action_controller/filters.rb:606:in`process’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/  
action\_controller/base.rb:391:in `process' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/ action_controller/base.rb:386:in`call’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/  
action\_controller/routing/route\_set.rb:438:in `call' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/ action_controller/dispatcher.rb:87:in`dispatch’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/  
action\_controller/dispatcher.rb:121:in `_call' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/ action_controller/dispatcher.rb:130:in`build\_middleware\_stack’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/  
action\_controller/string\_coercion.rb:25:in `call' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/ action_controller/string_coercion.rb:25:in`call’  
/usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/head.rb:9:in  
`call' /usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/ methodoverride.rb:24:in`call’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/  
action\_controller/params\_parser.rb:15:in `call' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/ action_controller/session/abstract_store.rb:128:in`call’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active\_record/  
query\_cache.rb:29:in `call' /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/ connection_adapters/abstract/query_cache.rb:34:in`cache’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active\_record/  
query\_cache.rb:9:in `cache' /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/ query_cache.rb:28:in`call’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active\_record/  
connection\_adapters/abstract/connection\_pool.rb:361:in `call' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/ action_controller/failsafe.rb:26:in`call’  
/usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/lock.rb:11:in  
`call' /usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/lock.rb:11:in`synchronize’  
/usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/lock.rb:11:in  
`call' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/ action_controller/dispatcher.rb:114:in`call’  
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/  
action\_controller/reloader.rb:34:in `run' /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/ action_controller/dispatcher.rb:108:in`call’  
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/rack/static.rb:  
31:in `call' /usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/urlmap.rb:47:in`call’  
/usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/urlmap.rb:41:in  
`each' /usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/urlmap.rb:41:in`call’  
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/rack/  
log\_tailer.rb:17:in `call' /usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/ content_length.rb:13:in`call’  
/usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/handler/  
webrick.rb:48:in `service' /usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in`service’  
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' /usr/local/lib/ruby/1.8/webrick/server.rb:173:in`start\_thread’  
/usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start' /usr/local/lib/ruby/1.8/webrick/server.rb:162:in`start\_thread’  
/usr/local/lib/ruby/1.8/webrick/server.rb:95:in `start' /usr/local/lib/ruby/1.8/webrick/server.rb:92:in`each’  
/usr/local/lib/ruby/1.8/webrick/server.rb:92:in `start' /usr/local/lib/ruby/1.8/webrick/server.rb:23:in`start’  
/usr/local/lib/ruby/1.8/webrick/server.rb:82:in `start' /usr/local/lib/ruby/gems/1.8/gems/rack-1.1.0/lib/rack/handler/ webrick.rb:14:in`run’  
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/commands/server.rb:  
111  
/usr/local/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:31:in  
`gem_original_require' /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in`require’  
script/server:3

From searching around this seems to have something to do with  
recursion, but I’m not sure how. I assumed the objects in the model  
would be file-like, so I tried to use the read method on them, but  
instead they returned a string or an array object. After that I tried  
to\_s and got the aforementioned error. Any help on this would be  
massively appreciated, 😃

---

<div class="post-metadata">

**Author:** ![Shef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@Shef](https://www.ruby-forum.com/u/Shef)\
**Post date:** [September 9, 2010, 9:17pm UTC](https://www.ruby-forum.com/t/file-upload-help/194741/2 "2010-09-09T21:17:31Z")

</div>

Shef wrote:

> I’m fairly new to rails

Please note that Rails is not Ruby. Problems with Rails really belong on  
a Rails mailing list. But in this case I see your problem is here:

> ```
> def corrections=(doc_field)
> self.corrections = doc_field.to_s
> end
> 
> ```

You’ve set up an infinite loop, of a method calling itself recursively,  
which in turn calls itself, which in turn calls itself… ad infinitum.  
Each call uses some memory, and so you must run out.

If this is an Active Record model, then use read\_attribute or  
write\_attribute to have direct access to the underlying data. (Look up  
“Facade Columns” in the index if you have the Agile Web D. with  
Rails book)

The non-Rails way to do what you want is something like this:

alias :old\_corrections= :corrections=

def corrections=(doc\_field)  
self.old\_corrections = doc\_field.to\_s  
end

But I’m not sure why you’re doing this - ActiveRecord adds the  
corrections= accessor for you, and if it’s a string column I’m fairly  
sure it will convert any value you pass into a string. So try leaving it  
out entirely.

Brian.

---

<div class="post-metadata">

**Author:** ![Shef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@Shef](https://www.ruby-forum.com/u/Shef)\
**Post date:** [September 9, 2010, 9:46pm UTC](https://www.ruby-forum.com/t/file-upload-help/194741/3 "2010-09-09T21:46:12Z")

</div>

Ok, I see, this was my goof. You see I was trying to do that because I  
am a moron, I gave both the method and the column the same name, I  
changed the name and it works like a charm, thank you so much for your  
help!

---

<div class="post-metadata">

**Author:** ![Shef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@Shef](https://www.ruby-forum.com/u/Shef)\
**Post date:** [September 9, 2010, 10:04pm UTC](https://www.ruby-forum.com/t/file-upload-help/194741/4 "2010-09-09T22:04:31Z")

</div>

Shef wrote:

> Ok, I see, this was my goof. You see I was trying to do that because I  
> am a moron, I gave both the method and the column the same name, I  
> changed the name and it works like a charm, thank you so much for your  
> help!

You do realise that ActiveRecord automatically creates a method with the  
same name as the column, at run time - so you don’t need to create one  
at all?

Even with an apparently empty definition like

class Foo \< ActiveRecord::Base  
end

you can still do

f = Foo.new  
f.bar = “hello”  
f.baz = “world”

(assuming that your database has a table called ‘foos’ with columns  
‘bar’ and ‘baz’)
