Serialized object behaves weird

Hi!

I got a class named EinsatzFilter which I serialized to session. Before
saving to session it works afterwards I keep getting the message:
“undefined method `to_s’ for #Person:0x38c6ab8”. “Person” is a from
ActiveRecord::Base inherited class.

Code:

class EinsatzFilter
include ApplicationHelper

attr_reader :personen, :monat, :projekte, :kunde
attr_writer :personen, :monat, :projekte, :kunde

ATTRIBUTES = [
[‘personen’, ‘person_id’, ‘Personen’, Person],
[‘projekte’, ‘projekt_id’, ‘Projekte’, Projekt],
]

def initialize(attrs = nil)
attrs.each {|k,v| self.send(k + ‘=’, v) } unless attrs.nil?
end

def to_s
ret = String.new
ATTRIBUTES.each {|attr|
vals = self.send(attr[0])
ret << (ret.empty? ? “” : "; ") + attr[2] + ": " +
make_list(vals.collect{|val| val.to_i },attr[3]) unless
vals.nil?
}
ret
end

Erzeugt aus dem Filter ein Array zur Ã?bergabe als :condition an

ActiveRecord::Base.find.

Rückgabewert könnte z.B. sein:

[“person_id in (?) and projekt_id in (?)”, [2, 3], [1, 2, 3]]

:call-seq:

find(:Einsatz, :conditions => @einsatz_filter.conditions)

def conditions
ret = Array.new(1, ‘’)
attrs = ATTRIBUTES.collect {|attr|
attr = [self.send(attr[0]), attr[1]]
}
attrs.each {|obj|
unless obj[0].nil?
ret[0] << " and " unless ret[0].empty?
ret[0] << “#{obj[1]} in (?)”
ret << obj[0].collect{|x| x.to_i}
end
}
return ret unless ret.length == 1
end

private
def make_list(arr, obj)
ret = ‘’
obj.find(arr).each {|p|
ret << ', ’ unless ret.length == 0
ret << p.to_s
}
ret
end
end

class Person < ActiveRecord::Base
has_many :Einsatz

def to_s
self.name
end
end

What’s my problem?

Greetings,
Alex.

I wrote:

def make_list(arr, obj)
ret = ‘’
obj.find(arr).each {|p|
ret << ', ’ unless ret.length == 0
ret << p.to_s
}
ret
end
end

I finally got it, but I am not clearly about the problem. I had to
change “obj.find” to “eval(obj).find”.

Please could someone explain that to me?

Alex.

If I ask before this exception appears I get the following array
including “to_s”:

“require_gem_with_options”, “after_find”, “cattr_accessor”, “const_set”,
“delete_observers”, “parents”, “read_methods”, “extend”,
“encode_quoted_value”, “validates_numericality_of”, “l_has_string?”,
“construct_calculation_sql”, “active_connection_name”,
“set_readonly_option!”, “after_update”, “set_inheritance_column”,
“deprecated_has_association_method”, “sum”, “serialize”, “frozen?”,
“pluralize_table_names”, “has_many”, “count_observers”, “to_a”,
“delete_all”, “extended_by”, “pluralize_table_names=”,
“before_validation”, “set_language”,
“deprecated_association_comparison_method”,
“thread_safe_scoped_methods”, “connected?”, “lock_optimistically”,
“const_defined?”, “l_time”, “validate_on_create”, “is_complex_yaml?”,
“has_and_belongs_to_many”, “lock_optimistically=”, “as_load_path”,
“reset_column_information_and_inheritable_attributes_for_all_subclasses”,
“unlock_mutex”, “class_inheritable_reader”, “sqlite3_connection”,
“destroy”, “table_exists?”, “to_s”, “nil?”,
“copy_instance_variables_from”, “clear_active_connection_name”,
“to_yaml_properties”, “after_validation”, “included_modules”,
“single_threaded_scoped_methods”, “respond_to?”, “set_locking_column”,
“current_scoped_methods”, “serialized_attributes”, “colorize_logging”,
“scope”, “validate_on_update”, “create!”, “postgresql_connection”,
“human_attribute_name”, “class_inheritable_writer”, “const_missing”,
“has_one”, “inspect”, “update_all”, “equal?”, “load”,
“colorize_logging=”, “reflect_on_aggregation”, “set_language_if_valid”,
“reset_column_information”, “find_on_conditions”,
“before_validation_on_create”, “find_by_sql”, “reset_locking_column”,
“remove_subclasses”, “reset_table_name”, “l_YesNo”,
“class_inheritable_array_writer”, “class_variables”, “belongs_to”,
“methods”, “daemonize”, “include?”, “timestamps_gmt”, “eql?”, “require”,
“retrieve_connection”, “method_defined?”, “columns”, “name”,
“after_validation_on_create”, “acts_as_tree”, “hash”, “calculate”,
“validates_size_of”, “timestamps_gmt=”, “remove_subclasses_of”,
“scoped_methods”, “condition_block?”, “find”,
“deprecated_find_in_collection_method”, “changed?”, “method”,
“default_timezone”, “evaluate_condition”, “to_yaml”,
“class_inheritable_hash_writer”, “create_reflection”,
“silence_warnings”, “singleton_methods”, “display”, “logger”,
“default_timezone=”, “compute_type”, “autoload”,
“public_method_defined?”, “ancestors”, “before_validation_on_update”,
“id”, “db2_connection”, “connection”, “class_name”,
“class_name_of_active_record_descendant”, “validates_uniqueness_of”,
“exists?”, “average”, “with_exclusive_scope”, “subclasses”,
“verification_timeout”, “logger=”, “base_class”,
“descends_from_active_record?”,
“deprecated_find_all_in_collection_method”, “connection=”,
“validates_each”, “sanitize”, “class_inheritable_accessor”,
“verification_timeout=”, “superclass”, “clear_active_connections!”,
“protected_methods”, “destroy_all”, “openbase_connection”,
“acts_as_list”, “extend_with_included_modules_from”,
“private_method_defined?”, “after_validation_on_update”,
“firebird_connection”, “id”, “replace_named_bind_variables”,
“subclasses_of”, “find_all”, “set_table_name”,
“deprecated_collection_create_method”, “l_yesno”, “allow_concurrency”,
“benchmark”, “autoload?”, “class_inheritable_array”, “private_methods”,
“enable_warnings”, “count_by_sql”, “instance_values”, “<=>”,
“protected_method_defined?”, “reset_subclasses”, “allow_concurrency=”,
“==”, “before_destroy”, “included_in_classes”, “object_id”,
“transaction”, “table_name”, “<”, “===”, “replace_bind_variables”,
“public_class_method”, “sanitize_sql”, “threaded_connections”,
“remove_class”, “sqlserver_connection”, “observers=”, “>”, “>=”,
“deprecated_collection_build_method”, “reset_primary_key”,
“table_name=”, “<=”, “instance_method”, “threaded_connections=”,
“class_inheritable_hash”, “verify_active_connections!”, “delete”,
“public_methods”, “silence_stderr”, “sanitize_conditions”, “=~”,
“instance_exec”, “l_age”, “after_destroy”, “columns_hash”, “type”,
“private_class_method”, “class_of_active_record_descendant”,
“current_language”, “create”, “encode64”, “validates_confirmation_of”,
“rails_original_const_missing”, “generate_read_methods”, “allocate”,
“inheritable_attributes”, “composed_of”, “instance_variables”,
“silence_stream”, “sqlite_connection”, “configurations”,
“generate_read_methods=”, “validate”, “class”, “module_eval”,
“instantiate”, “raise_if_bind_arity_mismatch”, “parent”,
“thread_safe_active_connections”, “configurations=”, “abstract_class”,
“ll”, “set_primary_key”, “l_lang_name”, “count”, “abstract_class=”,
“instance_variable_get”, “blank?”, “increment_counter”, “returning”,
“acts_as_nested_set”, “mattr_reader”, “column_names”,
“instance_methods”, “clone”, “decode_b”, “class_eval”,
“deprecated_collection_count_method”, “validates_format_of”,
“single_threaded_active_connections”, “ltry”, “inheritance_column”,
“update”, “after_initialize”, “decode64”, “schema_format”, “inherited”,
“reflections”, “write_inheritable_attribute”, “instance_variable_set”,
“inheritance_column=”, “send”, “with_options”, “l_date”,
“primary_key_prefix_type”, “schema_format=”, “mattr_writer”,
“abstract_class?”, “public_instance_methods”, “dup”,
“deprecated_add_association_relation”, “primary_key”,
“quote_bound_value”, “primary_key_prefix_type=”, “active_connections”,
“sequence_name”, “new”, “before_save”, “primary_key=”,
“validates_acceptance_of”, “`”, “class_inheritable_option”,
“write_inheritable_array”, “reflect_on_all_aggregations”, “suppress”,
“oracle_connection”, “decrement_counter”, “sequence_name=”,
“l_strftime”, “content_columns”, “protected_instance_methods”,
“changed”, “mattr_accessor”, “taint”,
“deprecated_remove_association_relation”, “validates_inclusion_of”,
“reset_sequence_name”, “after_save”, “lwr”, “send”, “find_first”,
“with_scope”, “mysql_connection”, “attr_protected”,
“connection_without_query_cache=”, “instantiate_with_callbacks”,
“to_json”, “table_name_prefix”, “require_gem”, “lock_mutex”,
“cattr_reader”, “private_instance_methods”, “notify_observers”,
“delegate”, “to_yaml_type”, “validates_exclusion_of”,
“record_timestamps”, “reloadable?”, “deprecated_has_collection_method”,
“table_name_prefix=”, “before_create”, “lwr_”, “instance_of?”,
“instance_eval”, “validates_presence_of”, “record_timestamps=”,
“read_inheritable_attribute”, “reflect_on_all_associations”,
“locking_column”, “protected_attributes”, “tainted?”, “scoped?”,
“establish_connection”, “l_datetime”, “column_methods_hash”,
“constants”, “quote”, “untaint”, “b64encode”, “validates_associated”,
“execute_grouped_calculation”, “extract_options_from_args!”,
“set_sequence_name”, “after_create”, “kind_of?”, “method_added”,
“validates_length_of”, “validate_find_options”,
“write_inheritable_hash”, “l”, “oci_connection”, “attr_accessible”,
“minimum”, “table_name_suffix”, “add_observer”, “cattr_writer”,
“const_get”, “reflect_on_association”, “delete_observer”, “freeze”,
“execute_simple_calculation”, “log_connections”, “table_name_suffix=”,
“silence”, “before_update”, “is_a?”, “reset_inheritable_attributes”,
“require_library_or_gem”, “accessible_attributes”, “maximum”,
“remove_connection”, “l_datetime_short”

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