# Variabili e nomi delle classi

**URL:** <https://www.ruby-forum.com/t/variabili-e-nomi-delle-classi/63925>\
**Category:** IT\
**Created:** [June 20, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/variabili-e-nomi-delle-classi/63925 "2006-06-20T18:44:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [June 20, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/variabili-e-nomi-delle-classi/63925/1 "2006-06-20T18:44:02Z")

</div>

Ciao raga

Volevo sottoporvi un quesito che purtroppo mi sta facendo rosicare una  
cifra e contestualmente sentire un troglodita

Scenario: ho la mia classe Attachment che è in has\_and\_belongs\_to\_many con  
2 o piu classi (nel mio caso Project e Visit)

Quando vado a definire il metodo “create” nell’attachments\_controller,  
mi porto dietro 2 parametri: referrer e foreignkey, cosi so che se vengo  
da un progetto, l’attachment lo devo mettere a lui e se vengo da una  
Visit stessa cosa

LA Domanda  
è:  
come posso sostituire il bruttissimo case - when e far interpretare la  
variabile come se fosse il nome dell’oggetto? Praticamente mi servirebbe  
l’equivalente del vecchio Eval

-------------------8\<-----------------8\<------------------8\<--------------

def create  
@foreignkey = params[:foreignkey]  
@referrer = params[:referrer]  
@oggetto = case @referrer  
when ‘Project’ then Project.find(@foreignkey)  
when ‘Visit’ then Visit.find(@foreignkey)  
else nil  
end

```
@attachment = Attachment.new(params[:attachment])
if @attachment.save
  @oggetto.attachments << @attachment
  flash[:notice] = 'Attachment was successfully created.'
  redirect_to :controller => Inflector.pluralize(@referrer), :action 

```

=\> ‘home’, :id =\> @foreignkey  
else  
render :action =\> ‘new’  
end  
end

spero la risposta non sia troppo banale mi sentirei ancora peggio 😃

bella!  
jeko

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [June 20, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/variabili-e-nomi-delle-classi/63925/2 "2006-06-20T20:05:17Z")

</div>

Stefano G. wrote:

> Scenario: ho la mia classe Attachment che è in has\_and\_belongs\_to\_many con 2 o piu classi (nel mio caso Project e Visit)

…

> def create  
> @foreignkey = params[:foreignkey]  
> @referrer = params[:referrer]  
> @oggetto = case @referrer  
> when ‘Project’ then Project.find(@foreignkey)  
> when ‘Visit’ then Visit.find(@foreignkey)  
> else nil  
> end  
> Potresti fare cosi:

@oggetto = Object.const\_get( params[:referrer] ).new

Poi, se sei su Rails 1.1, ci sono le polymorphic associations che  
consentono di associare ad un modello (e.g. Attachment) N diversi  
modelli.

nel tuo caso devi avere in Attachment i campi referrer\_id e  
referrer\_type poi:

class Project \< ActiveRecord::Base  
has\_many :attachments, :as =\> :referrer  
end

class Visit \< ActiveRecord::Base  
has\_many :attachments, :as =\> :referrer  
end

class Attachment \< ActiveRecord::Base  
belongs\_to :referrer, :polymorphic =\> true  
end

in questo modo puoi riferirti al referrer di un attachment come  
attachment.referrer e questo sara’ un oggetto del tipo giusto.

per esempio se fai:

progetto = Project.new  
attach = Attachment.new

puoi attaccare l’attach al progetto cosi:

attach.referrer = progetto

oppure

progetto.attachments \<\< attach

senza doverti preoccupare di impostare il tipo, questo ti puo aiutare  
soprattutto per le query.

ciao,  
Luca M.

–

## Web: [http://spazidigitali.com](http://spazidigitali.com) Email: mailto://removed\_email\_address@domain.invalid Skype: callto://l.mearelli
