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
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"