# Eval on LHS of Assignment

**URL:** <https://www.ruby-forum.com/t/eval-on-lhs-of-assignment/180557>\
**Category:** Rails\
**Created:** [December 21, 2009, 7:24pm UTC](https://www.ruby-forum.com/t/eval-on-lhs-of-assignment/180557 "2009-12-21T19:24:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [December 21, 2009, 7:24pm UTC](https://www.ruby-forum.com/t/eval-on-lhs-of-assignment/180557/1 "2009-12-21T19:24:19Z")

</div>

I want to set the values of several record fields from within an  
iterator where the field name is passed as a parameter. For example  
(conceptually):

# Line numbers are for reference only and are not a part of the code.

1. 

```
 record=Model.find(id)

```

2. 

```
 params.each do |key,value|

```

3. 

```
   eval('record.'+key)=value

```

4. 

```
 end

```

Apparently, having the eval on the LHS of an assignment doesn’t work.  
Can anyone please tell me what would work?

Thanks for any input.

```
     ... doug

```

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [December 21, 2009, 7:34pm UTC](https://www.ruby-forum.com/t/eval-on-lhs-of-assignment/180557/2 "2009-12-21T19:34:11Z")

</div>

Doug J. wrote:

> I want to set the values of several record fields from within an  
> iterator where the field name is passed as a parameter. For example  
> (conceptually):
> 
> # Line numbers are for reference only and are not a part of the code.
> 
> 1. 
> 
> ```
> record=Model.find(id)
> 
> ```
> 
> 2. 
> 
> ```
> params.each do |key,value|
> 
> ```
> 
> 3. 
> 
> ```
> eval('record.'+key)=value
> 
> ```
> 
> 4. 
> 
> ```
> end
> 
> ```
> 
> Apparently, having the eval on the LHS of an assignment doesn’t work.  
> Can anyone please tell me what would work?

record[key] = value

Note that this relies on the hash-like behavior of ActiveRecord. If you  
wanted to do this with a non-AR object, you could use  
record.send(“#{key}=”, value). eval is rarely necessary in Ruby.

However, in this case, you could just do  
record.update\_attributes(params).

> Thanks for any input.
> 
> ```
> ... doug
> 
> ```
> 
> –

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [December 22, 2009, 5:05am UTC](https://www.ruby-forum.com/t/eval-on-lhs-of-assignment/180557/3 "2009-12-22T05:05:00Z")

</div>

On Mon, Dec 21, 2009 at 1:23 PM, doug [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Apparently, having the eval on the LHS of an assignment doesn’t work.  
> Can anyone please tell me what would work?

First of all if you are doing this from a controller, you probably  
don’t want to use ALL of the parameters, but only those for the model,  
probably coming from a form,

For an active record model you can do:

# Assuming that the model really is called Model, and you are

following Rails conventions for the form:  
model\_params = params[:model] || []

model\_params.each do |key, value|  
record[key] = value  
end

or just

record.attributes=(model\_params, false)

The false overrides the protection of attributes, and probably isn’t a  
good idea in general. You normally don’t want to blindly mass assign  
any attributes the user throws at you. Consider the fact that the  
incoming url might be coming from a form submission from your app, or  
might be forged.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [Rick DeNatale - Developer - IBM | LinkedIn](http://www.linkedin.com/in/rickdenatale)

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
