Popolare una select

Ciao a tutti,
sono nei guai a causa di questa necessità :
devo popolare di una select.
Le option non sono altro che i nominativi di alcuni studenti che sono in
stato “attivo”.

Allora:

1- sul form ho scritto la seguente cosa:

<%= select(:timesheet, :pupil_id, Pupil.activePupils, {:prompt =>
“Seleziona uno studente”}, {:id => “search-text”})%>

2- sulla classe Pupil (modello) ho scritto il seguente metodo:

def activePupils
Pupil.all(:conditions => [“status = ? AND tiposervizi = ?”, STATUS[1],
SERVIZI[0]], :select => “id, nome, cognome”)
end

Ma quando lancio la “new” Rails mi spernacchia: “undefined method
`activePupils’ for #Class:0x43e9a80

Questo è il track completo:
C:/Ruby191/lib/ruby/gems/1.9.1/gems/activerecord-2.3.8/lib/active_record/base.rb:1994:in
method_missing' F:/rubyProjects/fg/app/views/timesheets/_form.html.erb:31:inblock in
_run_erb_app47views47timesheets47_form46html46erb_locals_form_object’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/helpers/form_helper.rb:499:in
fields_for' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/helpers/form_helper.rb:282:inform_for’
F:/rubyProjects/fg/app/views/timesheets/_form.html.erb:1:in
_run_erb_app47views47timesheets47_form46html46erb_locals_form_object' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/renderable.rb:34:inblock in render’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/base.rb:306:in
with_template' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/renderable.rb:30:inrender’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/renderable_partial.rb:20:in
block in render' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:26:inblock in benchmark’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in
block in ms' C:/Ruby191/lib/ruby/1.9.1/benchmark.rb:309:inrealtime’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in
ms' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:26:inbenchmark’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/renderable_partial.rb:19:in
render' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/template.rb:205:inrender_template’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/renderable_partial.rb:44:in
render_partial' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/partials.rb:184:inrender_partial’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/base.rb:267:in
render' F:/rubyProjects/fg/app/views/timesheets/new.html.erb:2:in_run_erb_app47views47timesheets47new46html46erb’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/renderable.rb:34:in
block in render' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/base.rb:306:inwith_template’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/renderable.rb:30:in
render' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/template.rb:205:inrender_template’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/base.rb:265:in
render' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/base.rb:348:in_render_with_layout’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_view/base.rb:262:in
render' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/base.rb:1250:inrender_for_file’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/base.rb:942:in
render' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:51:inblock in render_with_benchmark’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in
block in ms' C:/Ruby191/lib/ruby/1.9.1/benchmark.rb:309:inrealtime’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in
ms' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:51:inrender_with_benchmark’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:135:in
block in custom' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:179:incall’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:179:in
block in respond' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:173:ineach’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:173:in
respond' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/mime_responds.rb:107:inrespond_to’
F:/rubyProjects/fg/app/controllers/timesheets_controller.rb:23:in new' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/base.rb:1331:inperform_action’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/filters.rb:617:in
call_filters' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/filters.rb:610:inperform_action_with_filters’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:68:in
block in perform_action_with_benchmark' C:/Ruby191/lib/ruby/gems/1.9.1/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:inblock in ms’
C:/Ruby191/lib/ruby/1.9.1/benchmark.rb:309:in realtime' C:/Ruby191/lib/ruby/gems/1.9.1/gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:inms’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:68:in
perform_action_with_benchmark' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/rescue.rb:160:inperform_action_with_rescue’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/flash.rb:151:in
perform_action_with_flash' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/base.rb:532:inprocess’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/filters.rb:606:in
process_with_filters' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/base.rb:391:inprocess’
C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/base.rb:386:in
call' C:/Ruby191/lib/ruby/gems/1.9.1/gems/actionpack-2.3.8/lib/action_controller/routing/route_set.rb:438:incall’

Aitaaaaaa !
Dove sbaglio ?

Grazie a tutti quelli che vorranno perdere un po’ di tempo su sta
cosa…

Ciao

def activePupils
Pupil.all(:conditions => [“status = ? AND tiposervizi = ?”, STATUS[1],
SERVIZI[0]], :select => “id, nome, cognome”)
end

per usarlo così il metodo deve essere :

def self.activePupils
Pupil.all(:conditions => [“status = ? AND tiposervizi = ?”, STATUS[1],
SERVIZI[0]], :select => “id, nome, cognome”)
end

altrimenti puoi fare una named_scope :

(non so se puoi passargli select però penso di sì…)

named_scope :activePupils, :select => “id, nome, cognome”, lambda {
|val1, val2|
{ :conditions => [“status = ? AND tiposervizi = ?”, val1, val2] }
}

Grazie Alessandro !
Adesso funziona…

In effetti avrei dovuto pensarci: il metodo è statico…

Alessandro S. wrote:

def activePupils
Pupil.all(:conditions => [“status = ? AND tiposervizi = ?”, STATUS[1],
SERVIZI[0]], :select => “id, nome, cognome”)
end

per usarlo così il metodo deve essere :

def self.activePupils
Pupil.all(:conditions => [“status = ? AND tiposervizi = ?”, STATUS[1],
SERVIZI[0]], :select => “id, nome, cognome”)
end

altrimenti puoi fare una named_scope :

(non so se puoi passargli select però penso di sì…)

named_scope :activePupils, :select => “id, nome, cognome”, lambda {
|val1, val2|
{ :conditions => [“status = ? AND tiposervizi = ?”, val1, val2] }
}

Probabilmente, come diceva Alessandro, in questo caso un named scope è
ancora più appropriato.

in pupil model:

named_scope :active, :conditions => [“status = ? AND tiposervizi = ?”,
STATUS[1], SERVIZI[0]], :select => “id, nome, cognome”)

così nella view puoi usare Pupil.active o @pupils.active nel caso
dichiari la variabile @pupils nel controller. Il named scope ha proprio
questa finalità , ed è un bel po’ più leggibile e mantenibile.

Per un tutorial dai un’occhiata sul sempre ottimo railscasts:
#108 named_scope - RailsCasts

An Car wrote:

Grazie Alessandro !
Adesso funziona…

In effetti avrei dovuto pensarci: il metodo è statico…

Alessandro S. wrote:

def activePupils
Pupil.all(:conditions => [“status = ? AND tiposervizi = ?”, STATUS[1],
SERVIZI[0]], :select => “id, nome, cognome”)
end

per usarlo così il metodo deve essere :

def self.activePupils
Pupil.all(:conditions => [“status = ? AND tiposervizi = ?”, STATUS[1],
SERVIZI[0]], :select => “id, nome, cognome”)
end

altrimenti puoi fare una named_scope :

(non so se puoi passargli select però penso di sì…)

named_scope :activePupils, :select => “id, nome, cognome”, lambda {
|val1, val2|
{ :conditions => [“status = ? AND tiposervizi = ?”, val1, val2] }
}