2011-09-28 24 views

Respuesta

20

Sí, puede ampliar sus modelos sobre la marcha. Por ejemplo:

# GET /agents 
# GET /agents.xml 
def index 
    @agents = Agent.all 

    # Here we modify the particular models in the @agents array. 

    @agents.each do |agent| 
    agent.class_eval do 
     attr_accessor :foo 
     attr_accessor :bar 
    end 
    end 

    # And then we can then use "foo" and "bar" as extra attributes 

    @agents.each do |agent| 
    agent.foo = 4 
    agent.bar = Time.now 
    end 

    respond_to do |format| 
    format.html # index.html.erb 
    format.xml { render :xml => @agents} 
    end 
end 

En el código de la vista, puede hacer referencia a foo y bar como lo haría con otros atributos.

+0

Gracias por su respuesta. Eventualmente lo resolví, pero espero que esto ayude a otros. Es bueno saber que las personas todavía están respondiendo. – nexar

Cuestiones relacionadas