Problem with "can't dump anonymous class Class"

I submitted this earlier, but the web forums went down and I’ve screwed
up
the thread, so I’m starting over.

I’m trying to built a document upload system. The system has a main
Document model with different subclasses for different types of
documents.
With the code below, if I attempt to create a Document, it works fine,
but
if I attempt to use one of the subclasses, I get the error:

can't dump anonymous class Class

With an Application Trace of:

C:/Ruby/lib/ruby/1.8/yaml/rubytypes.rb:6:in `to_yaml'

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connect
ion_adapters/abstract/quoting.rb:22:in `quote’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connect
ion_adapters/mysql_adapter.rb:123:in `quote’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb
:1305:in `quote_bound_value’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb
:1287:in `replace_bind_variables’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb
:1287:in `replace_bind_variables’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb
:1276:in `sanitize_sql’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb
:1062:in `add_conditions!’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb
:1012:in `construct_finder_sql’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb
:924:in `find_every’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb
:918:in `find_initial’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb
:380:in `find’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validat
ions.rb:528:in `validates_uniqueness_of’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validat
ions.rb:302:in `validates_each’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validat
ions.rb:299:in `validates_each’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validat
ions.rb:794:in `run_validations’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validat
ions.rb:788:in `run_validations’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validat
ions.rb:752:in `valid_without_callbacks’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/callbac
ks.rb:306:in `valid?’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validat
ions.rb:723:in `save_without_transactions’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transac
tions.rb:126:in `save’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connect
ion_adapters/abstract/database_statements.rb:51:in `transaction’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transac
tions.rb:91:in `transaction’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transac
tions.rb:118:in `transaction’

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transac
tions.rb:126:in save' #{RAILS_ROOT}/app/controllers/enrollment_admin_controller.rb:34:insave_global_document’
-e:4

Any ideas on how to fix this? If I change GlobalDocument to Document
below,
it works fine.

Here is the relevant code:

** Models **

class Document < ActiveRecord::Base

attr_accessor :uploaded_file

def after_create
if !File.exists?(File.dirname(self.file_path))
Dir.mkdir(File.dirname(self.file_path))
end

if @uploaded_file.instance_of?(Tempfile)
  FileUtils.copy(@uploaded_file.local_path, self.file_path)
else
  File.open(self.file_path, "wb") { |f| f.write(@uploaded_file.read) 

}
end
end

def after_destroy
if File.exists?(self.file_path)
File.delete(self.file_path)
end
end

def file=(uploaded_file)
@uploaded_file = uploaded_file
self.file_path = sanitize_filename(@uploaded_file.original_filename)
self.content_type = @uploaded_file.content_type
end

private
def new_filename(file_name)
idx = file_name.rindex “.”
self.name.gsub(/[^\w._]/, ‘_’) + file_name[idx, file_name.length -
idx]
end

def sanitize_filename(file_name)

File.expand_path("#{RAILS_ROOT}/public/documents/#{new_filename(file_name)}"
)
end

end

class GlobalDocument < Document

validates_uniqueness_of :name, :scope => :type

private
def sanitize_filename(file_name)

File.expand_path("#{RAILS_ROOT}/public/documents/global/#{new_filename(file_
name)}")
end

end

** Controller **

def documents
@new_document = GlobalDocument.new
end

def save_global_document
doc = GlobalDocument.new(params[:new_document])
if doc.save
flash[:notice] = “#{doc.name} was saved”
else
flash[:error] = doc.errors.full_messages.join("
")
end
redirect_to :action => :documents
end

** View **

documents.rhtml

<%= form_tag({ :action => :save_global_document }, { :multipart => 

true
}) %>














Name: <%= text_field :new_document, :name, :class => “fixed”
%>
File: <%= file_field :new_document, :file, :class => “fixed”
%>
<%= submit_tag “Upload file” %>

<%= end_form_tag %>

If found the problem but don’t understand it. The problem is the
‘validates_uniqueness_of :name, :scope => :type’ line.

If I take it out, it works
If I make it ‘validates_uniqueness_of :name’, it works
If I make it ‘validates_uniqueness_of :name, :scope => :type’, it
doesn’t
work.

Why would this happen?

Stephen G.

To answer my own question…

With STI, ‘validates_uniqueness_of :name’ is limited to the subclass, so
doing ‘:scope => :type’ isn’t needed.

Stephen G.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs