# Assigning values of an object when it is created based on mysql queries

**URL:** <https://www.ruby-forum.com/t/assigning-values-of-an-object-when-it-is-created-based-on-mysql-queries/186167>\
**Category:** Rails\
**Created:** [April 1, 2010, 3:02pm UTC](https://www.ruby-forum.com/t/assigning-values-of-an-object-when-it-is-created-based-on-mysql-queries/186167 "2010-04-01T15:02:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 1, 2010, 3:02pm UTC](https://www.ruby-forum.com/t/assigning-values-of-an-object-when-it-is-created-based-on-mysql-queries/186167/1 "2010-04-01T15:02:28Z")

</div>

I want to use a value entered in by a user in an object’s form to look  
up another value in a reference table and assign that value to the  
corresponding attribute in the object. How do I do that?

Right now I have in my controller create method:

```
if @number = Work.find_by_sql( "SELECT inv FROM

```

samples\_development.works WHERE worknbr REGEXP ‘" +  
@sample[‘work\_number’] + "’;" )  
@sample.inv = @number  
end

But it assigns something odd to the number attribute that looks like  
this:

!ruby/object:Work attributes: inv: INV 1986/MR 690/2201  
attributes\_cache: {} -

Should I be doing this in the model instead?

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 1, 2010, 3:09pm UTC](https://www.ruby-forum.com/t/assigning-values-of-an-object-when-it-is-created-based-on-mysql-queries/186167/2 "2010-04-01T15:09:29Z")

</div>

On Apr 1, 2:00 pm, ES [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I want to use a value entered in by a user in an object’s form to look  
> up another value in a reference table and assign that value to the  
> corresponding attribute in the object. How do I do that?
> 
> Right now I have in my controller create method:
> 
> ```
> if @number = Work.find_by_sql( "SELECT inv FROM
> 
> ```
> 
> samples\_development.works WHERE worknbr REGEXP ‘" +  
> @sample[‘work\_number’] + "’;" )

find\_by\_sql always returns an array of instances of the corresponding  
class (Work in this case) no matter what the select clause is. The  
columns in the result set are all there as attributes though

Fred

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 1, 2010, 3:31pm UTC](https://www.ruby-forum.com/t/assigning-values-of-an-object-when-it-is-created-based-on-mysql-queries/186167/3 "2010-04-01T15:31:43Z")

</div>

Ok, thanks! So would this be the way to do it?

```
@i = Work.find_by_sql( "SELECT inv FROM samples_development.works

```

WHERE worknbr REGEXP ‘" + @sample[‘work\_number’] + "’;" )  
@sample.inv = @i[‘inv’]

I keep getting “can’t convert String into Integer”

On Apr 1, 3:07 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 1, 2010, 3:34pm UTC](https://www.ruby-forum.com/t/assigning-values-of-an-object-when-it-is-created-based-on-mysql-queries/186167/4 "2010-04-01T15:34:39Z")

</div>

No, that was wrong, it returns an array, so I had to add .first :

```
@i = Work.find_by_sql( "SELECT inv FROM samples_development.works

```

WHERE worknbr REGEXP ‘" + @sample[‘work\_number’] + "’;" )  
@sample.inv = @i.first[‘inv’]
