Hola paolo, entiendo que lo que intenta hacer el metodo es obtener el
attributo "degree" del registro mas nuevo verdad ?, si usas mysql como
backend en mi experiencia es menos costoso ordenar por fecha en forma
descendiente y hacer un limit 1, pero independiente del backend que uses
podes hacer lo mismo con tan solo hacer:

   def current_degree
       degree_changes.order('date DESC').first.try(:degree)
   end

otro tip respecto a tu codigo es que podes usar el metodo maximum('date')
para obtener el maximo.

Saludos.



2011/5/24 Paolo Loran <[email protected]>

> Buenas tardes, pido perdón de antemano por si cometo errores tontos, es que
> soy bastante nuevo en esto.
> El tema es asi, tengo el siguiente metodo:
>
> ______________________________________________
> def current_degree
>     current_date = degree_changes.find(:first, :select => 'max(date) as
> max').max
>     change = degree_changes.where(:date => current_date).first
>     change.degree
>   end
> __________________________________________
> y en la vista quiero mostrarlo:
> .....
> <% @students.each do |student| %>
>   <tr>
>     <td><%= student.surname %></td>
>     <td><%= student.name %></td>
>     <td><%= student.current_degree %></td>
>     .........
> __________________________________________
> Pero me da el siguiente error:
>
> undefined method `degree' for nil:NilClass
>
> Extracted source (around line *#25*):
>
> 22:   <tr>
> 23:     <td><%= student.surname %></td>
> 24:   <td><%= student.name %></td>
> 25:   <td><%= student.current_degree %></td>
> 26:     <td><%= student.birthday %></td>
> 27:     <td><%= student.dni %></td>
> 28:   <td><%= student.phone_number %></td>
> _____________________________________________________
> Tengo las clases relacionadas con has_many y belongs_to
> tambien lo probe mediante una consola(rails c)
>
> que podra ser?
>
> salud!!!
> poli
>
>
>
>
> _______________________________________________
> Ruby mailing list
> [email protected]
> http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar
>
>


-- 
Geronimo Diaz
v1 | services
[email protected]
[email protected]
_______________________________________________
Ruby mailing list
[email protected]
http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar

Responder a