Operation on data for model

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.

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

Is this best done in model than in controller?

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?

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

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.

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”