# Handling multiple models DRYly-- using eval, but better way?

**URL:** <https://www.ruby-forum.com/t/handling-multiple-models-dryly-using-eval-but-better-way/138698>\
**Category:** Rails\
**Created:** [May 26, 2008, 10:50pm UTC](https://www.ruby-forum.com/t/handling-multiple-models-dryly-using-eval-but-better-way/138698 "2008-05-26T22:50:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![djlewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@djlewis](https://www.ruby-forum.com/u/djlewis)\
**Post date:** [May 26, 2008, 10:50pm UTC](https://www.ruby-forum.com/t/handling-multiple-models-dryly-using-eval-but-better-way/138698/1 "2008-05-26T22:50:27Z")

</div>

I have a bunch of models (about 30) that are all handled roughly the  
same way despite having different fields, text, etc (there’s a big  
data structure to drive all that). I’m trying to be DRY and handle  
them generically. The best way I’ve come up with is occasional use of  
eval. That leads to controller code like this to, say, add a bunch of  
records to the db table…

```
tbl = q.code # the particular model's id
tblp = tbl.pluralize
tblc = tbl.camelize
@user_records = eval( "@user.#{tblp}")
if @user_records.empty?
  stuff.each do |sh|
    rcd = eval( "#{tblc}.new")
    # code here to create new rcd from sh
    eval( "@user.#{tblp} << rcd")
  end
end

```

and this, in the corresponding view…

\<% form\_for @page.code.to\_s + “[]”, :url =\> {:action  
=\> :question, :id =\> @page.code} do |form| %\>  
\<% @user\_records.each do |rcd| %\>  
\<% eval “@#{@page.code}= rcd” %\>  
\<% @columns.each\_column do |col| %\>  
\<%= "

#{qfield( form, col)}-- column = #{col.code} /  
#{col.type} / #{col.text.to\_s}

" %\>  
\<% end %\>  
\<% end %\>  
\<% end %\>

where qfield is a helper to construct the appropriate html for the  
column according to its type and other characteristics.

My question – is there a better way? Rails seems to want an actual  
assignment to the @model variable to drive the view, so just pointing  
at it with a more generic variable won’t work (I think I tried this,  
but maybe I didn’t do it right). That’s the reason behind the third  
line of the view.

And is there a way to treat the model’s class dynamically without  
eval, in – rcd = eval( “#{tblc}.new”)

Thanks. --David.

---

<div class="post-metadata">

**Author:** ![djlewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@djlewis](https://www.ruby-forum.com/u/djlewis)\
**Post date:** [May 26, 2008, 11:02pm UTC](https://www.ruby-forum.com/t/handling-multiple-models-dryly-using-eval-but-better-way/138698/2 "2008-05-26T23:02:12Z")

</div>

Hi –

On Mon, 26 May 2008, djlewis wrote:

> tblc = tbl.camelize  
> @user\_records = eval( “@user.#{tblp}”)

```
  @user_records = @user.send(tblp)

```

> if @user\_records.empty?  
> stuff.each do |sh|  
> rcd = eval( “#{tblc}.new”)

```
      rcd = tblc.constantize.new

```

> ```
> # code here to create new rcd from sh
> eval( "@user.#{tblp} << rcd")
> 
> ```

```
      @user.send(tblp) << rcd

```

> ```
> end
> 
> ```
> 
> end
> 
> and this, in the corresponding view…
> 
> \<% form\_for @page.code.to\_s + “[]”, :url =\> {:action  
> =\> :question, :id =\> @page.code} do |form| %\>  
> \<% @user\_records.each do |rcd| %\>  
> \<% eval “@#{@page.code}= rcd” %\>

```
  <% instance_variable_set("@#{@page.code}", rcd) %>

```

> My question – is there a better way? Rails seems to want an actual  
> assignment to the @model variable to drive the view, so just pointing  
> at it with a more generic variable won’t work (I think I tried this,  
> but maybe I didn’t do it right). That’s the reason behind the third  
> line of the view.
> 
> And is there a way to treat the model’s class dynamically without  
> eval, in – rcd = eval( “#{tblc}.new”)

See above for alternatives to all of your eval uses (untested). The  
methods I’ve used are all Ruby methods with the exception of  
constantize, which is from ActiveSupport.

David

–  
Rails training from David A. Black and Ruby Power and Light:  
INTRO TO RAILS June 9-12 Berlin  
ADVANCING WITH RAILS June 16-19 Berlin  
INTRO TO RAILS June 23-26 London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!
