# Popolare una select

**URL:** <https://www.ruby-forum.com/t/popolare-una-select/191057>\
**Category:** IT\
**Created:** [July 3, 2010, 5:24pm UTC](https://www.ruby-forum.com/t/popolare-una-select/191057 "2010-07-03T17:24:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![punkaruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/49beb7/32.png) [@punkaruby](https://www.ruby-forum.com/u/punkaruby)\
**Post date:** [July 3, 2010, 5:24pm UTC](https://www.ruby-forum.com/t/popolare-una-select/191057/1 "2010-07-03T17:24:52Z")

</div>

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:in`block 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:in`form\_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:in`block 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:in`render’  
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:in`block 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:in`realtime’  
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:in`benchmark’  
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:in`render\_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:in`render\_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:in`with\_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:in`render\_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:in`render\_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:in`block 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:in`realtime’  
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:in`render\_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:in`call’  
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:in`each’  
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:in`respond\_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:in`perform\_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:in`perform\_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:in`block 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:in`ms’  
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:in`perform\_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:in`process’  
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:in`process’  
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:in`call’

Aitaaaaaa !  
Dove sbaglio ?

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

Ciao

---

<div class="post-metadata">

**Author:** ![punkaruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/49beb7/32.png) [@punkaruby](https://www.ruby-forum.com/u/punkaruby)\
**Post date:** [July 3, 2010, 10:49pm UTC](https://www.ruby-forum.com/t/popolare-una-select/191057/2 "2010-07-03T22:49:05Z")

</div>

> 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] }  
}

---

<div class="post-metadata">

**Author:** ![punkaruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/49beb7/32.png) [@punkaruby](https://www.ruby-forum.com/u/punkaruby)\
**Post date:** [July 4, 2010, 11:32am UTC](https://www.ruby-forum.com/t/popolare-una-select/191057/3 "2010-07-04T11:32:11Z")

</div>

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] }  
> }

---

<div class="post-metadata">

**Author:** ![punkaruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/49beb7/32.png) [@punkaruby](https://www.ruby-forum.com/u/punkaruby)\
**Post date:** [July 12, 2010, 8:56pm UTC](https://www.ruby-forum.com/t/popolare-una-select/191057/4 "2010-07-12T20:56:48Z")

</div>

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](http://railscasts.com/episodes/108-named-scope)

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] }  
> > }
