Forum: Ruby on Rails operation on data for model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
23368cea7809b2ced39bc4d48f32dca0?d=identicon&s=25 max (Guest)
on 2007-03-29 13:36
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.
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-03-29 14:09
(Received via mailing list)
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
23368cea7809b2ced39bc4d48f32dca0?d=identicon&s=25 max (Guest)
on 2007-03-29 16:38

Is this best done in model than in controller?
23368cea7809b2ced39bc4d48f32dca0?d=identicon&s=25 max (Guest)
on 2007-03-29 17:10
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?
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-03-29 17:23
(Received via mailing list)
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.
23368cea7809b2ced39bc4d48f32dca0?d=identicon&s=25 max (Guest)
on 2007-03-30 06:13
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
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-03-30 13:33
(Received via mailing list)
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"
This topic is locked and can not be replied to.