# Operation on data for model

**URL:** <https://www.ruby-forum.com/t/operation-on-data-for-model/90514>\
**Category:** Rails\
**Created:** [March 29, 2007, 1:36pm UTC](https://www.ruby-forum.com/t/operation-on-data-for-model/90514 "2007-03-29T13:36:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![max](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@max](https://www.ruby-forum.com/u/max)\
**Post date:** [March 29, 2007, 1:36pm UTC](https://www.ruby-forum.com/t/operation-on-data-for-model/90514/1 "2007-03-29T13:36:48Z")

</div>

Hi  
First let me mention that i am new to rails.  
In my application there is a form for entering product details for  
bidding. I have a view and all the data is entered into a form.  
In the database there is a unique\_id which is generated for each  
product(this is for internal use within the company). The unique id is  
generated using data from 2 fields and another generated number. all 3  
needs to be concatenated.  
My questions:

1. where is the best place to perform the operation.  
I assume it is in the controller
2. How do i do it. How do I take the form data and concatenate all the  
values, so that the unique id is inserted into the database.

---

<div class="post-metadata">

**Author:** ![max](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@max](https://www.ruby-forum.com/u/max)\
**Post date:** [March 29, 2007, 2:09pm UTC](https://www.ruby-forum.com/t/operation-on-data-for-model/90514/2 "2007-03-29T14:09:43Z")

</div>

you can handle this all in the model

since i don’t know your attribute names, i’ll just use simple ones,  
foo, bar and the unique id attribute will be unique\_ident

class Model \< AR::Base

before\_create :generate\_unique\_ident

def generate\_unique\_ident  
unique\_ident = foo + bar + self.generate\_some\_number  
end

private  
def self.generate\_some\_number  
…  
end

validates\_presence\_of :foo  
validates\_presence\_of :bar  
end

then just save your model as usual

---

<div class="post-metadata">

**Author:** ![max](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@max](https://www.ruby-forum.com/u/max)\
**Post date:** [March 29, 2007, 4:38pm UTC](https://www.ruby-forum.com/t/operation-on-data-for-model/90514/3 "2007-03-29T16:38:42Z")

</div>

Is this best done in model than in controller?

---

<div class="post-metadata">

**Author:** ![max](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@max](https://www.ruby-forum.com/u/max)\
**Post date:** [March 29, 2007, 5:10pm UTC](https://www.ruby-forum.com/t/operation-on-data-for-model/90514/4 "2007-03-29T17:10:47Z")

</div>

Is this best done in model than in controller?  
Is it possible to retrieve some data from the db according to a field?

Eg: let us assume foo, bar and unique\_ident as above.  
now bar contains a primary key of a table which contains actual data.  
foo = 137874  
bar = 1  
unique\_ident = ss137874, because according to table  
bar\_table  
id value  
1 ss  
2 dd

now can a retrieve the value according to the value of bar which is  
given in the view?

---

<div class="post-metadata">

**Author:** ![max](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@max](https://www.ruby-forum.com/u/max)\
**Post date:** [March 30, 2007, 6:13am UTC](https://www.ruby-forum.com/t/operation-on-data-for-model/90514/5 "2007-03-30T06:13:31Z")

</div>

ok. let me start from the begining.  
3 fields of concern here are product\_code, product\_category,  
unique\_ident.  
In the view product\_category is selected through a dropdown. Its values  
are integer, which is id of the category table.  
given below is sample values of category table  
categories  
id category category\_code  
1 electronics el  
2 cars ca

now, sample values input by the user  
product\_code : 333  
product\_category : 2

in this case the unique\_ident should be product\_code + the category\_code  
of the corresponding product\_category  
i.e: ca333

I hope i am clear this time

---

<div class="post-metadata">

**Author:** ![max](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@max](https://www.ruby-forum.com/u/max)\
**Post date:** [March 29, 2007, 5:23pm UTC](https://www.ruby-forum.com/t/operation-on-data-for-model/90514/6 "2007-03-29T17:23:19Z")

</div>

sorry, you’ve confused me.

foo and bar where my mock columns on your model. i don’t understand  
where this bar table is coming from.

perhaps you need to provide more info on your situtation.

---

<div class="post-metadata">

**Author:** ![max](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e5b9ba/32.png) [@max](https://www.ruby-forum.com/u/max)\
**Post date:** [March 30, 2007, 1:33pm UTC](https://www.ruby-forum.com/t/operation-on-data-for-model/90514/7 "2007-03-30T13:33:09Z")

</div>

well, what you could do in this case is just make it a ‘psuedo  
attribute’ and you don’t even have to store it in the database

class Product

def unique\_ident  
category.category\_code + product\_code  
end  
end

so in this example say you have

products  
id: 1  
product\_code: 333  
category\_id: 2

categories  
id: 2  
category: cars  
category\_code: ca

p = Product.find(1)  
p.unique\_ident # =\> “ca333”
