File upload help

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:incorrections=’
/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:inassign_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:inassign_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:ininitialize’
/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:inupload’
/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:inperform_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:inperform_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:inms’
/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:inms’
/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:inperform_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:insend’
/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:inprocess’
/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:incall’
/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:indispatch’
/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:inbuild_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:incall’
/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:incall’
/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:incall’
/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:incache’
/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:incall’
/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:incall’
/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:insynchronize’
/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:incall’
/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:incall’
/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:incall’
/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:incall’
/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:incall’
/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:inservice’
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in run' /usr/local/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
/usr/local/lib/ruby/1.8/webrick/server.rb:162:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
/usr/local/lib/ruby/1.8/webrick/server.rb:95:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:92:ineach’
/usr/local/lib/ruby/1.8/webrick/server.rb:92:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:23:instart’
/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:inrun’
/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:inrequire’
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, :smiley:

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.

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!

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’)