2008/5/15 Gustavo G. [email protected]:
Hola.
Quisiera saber si alguien sabe porque no se puede utilizar la palabra
“display” como nombre de un metodo en el controller. He estado haciendo
un ejercicio de un tutorial para aprender en ruby, donde toma valores
nombre y edad de un view y los envia a otro view llamado display y es
ahi cuando explota diciendo que estoy utilizando un “nil object”.
El valor de las columnas de una fila de una tabla accedida a través de
ActiveRecord, se realiza mediante esta función:
activerecord-2.0.2/lib/active_record/attribute_methods.rb
181 def method_missing(method_id, *args, &block)
182 method_name = method_id.to_s
183
184 # If we haven’t generated any methods yet, generate them, then
185 # see if we’ve created the method we’re looking for.
186 if !self.class.generated_methods?
187 self.class.define_attribute_methods
188 if self.class.generated_methods.include?(method_name)
189 return self.send(method_id, *args, &block)
190 end
191 end
192
193 if self.class.primary_key.to_s == method_name
194 id
195 elsif md = self.class.match_attribute_method?(method_name)
196 attribute_name, method_type = md.pre_match, md.to_s
197 if @attributes.include?(attribute_name)
198 send(“attribute#{method_type}”, attribute_name, *args,
&block)
199 else
200 super
201 end
202 elsif @attributes.include?(method_name)
203 read_attribute(method_name)
204 else
205 super
206 end
207 end
Tiramos del hilo
activerecord-2.0.2/lib/active_record/attribute_methods.rb
59 # generates all the attribute related methods for columns in
the database
60 # accessors, mutators and query methods
61 def define_attribute_methods
62 return if generated_methods?
63 columns_hash.each do |name, column|
64 unless instance_method_already_implemented?(name)
65 if self.serialized_attributes[name]
66 define_read_method_for_serialized_attribute(name)
67 else
68 define_read_method(name.to_sym, name, column)
69 end
70 end
71
72 unless instance_method_already_implemented?(“#{name}=”)
73 define_write_method(name.to_sym)
74 end
75
76 unless instance_method_already_implemented?(“#{name}?”)
77 define_question_method(name)
78 end
79 end
80 end
y por último
activerecord-2.0.2/lib/active_record/attribute_methods.rb
82 # Check to see if the method is defined in the model or any
of its subclasses that also derive from ActiveRecord.
83 # Raise DangerousAttributeError if the method is defined by
ActiveRecord though.
84 def instance_method_already_implemented?(method_name)
85 return true if method_name =~ /^id(=$|?$|$)/
86 @_defined_class_methods ||=
Set.new(ancestors.first(ancestors.index(ActiveRecord::Base)).collect!
{ |m| m.p ublic_instance_methods(false) |
m.private_instance_methods(false) |
m.protected_instance_methods(false) }.flatten)
87 @@_defined_activerecord_methods ||=
Set.new(ActiveRecord::Base.public_instance_methods(false) |
ActiveRecord::Base. private_instance_methods(false) |
ActiveRecord::Base.protected_instance_methods(false))
88 raise DangerousAttributeError, “#{method_name} is defined
by ActiveRecord” if @@_defined_activerecord_methods.inclu
de?(method_name)
89 @_defined_class_methods.include?(method_name)
90 end
Vamos que tira de public_method_instance_methods
protected_instance_methods y private_instance_methods. (definidas en
el core de ruby, en el apartado Module:
class Module - RDoc Documentation )
Con buen criterio (en mi opinión), active record crea atributos de la
clase para todas las columnas cuyos correspondientes métodos no
existan.
display es un método básico de ruby, definido en Object
http://ruby-doc.org/core/classes/Object.html#M000375
Y salvo que me haya equivocado, creo que la cosa va por ahÃ.
Un Saludo y buenas noches.