I don’t know how to handle this kind of situation the Rails way, please
create table products (id int not null auto_increment, name
varchar(255), primary key (id));
create table batches (id int not null auto_increment, product_id int
not null, quantity int not null, price decimal(10,2) not null, primary
key (id), foreign key (product_id) references products(id));
create table serial_numbers (id int not null auto_increment, batch_id
int not null, sn varchar(255), status char(1), primary key (id),
foreign key (batch_id) references batches(id));
class Batch < ActiveRecord::Base
class SerialNumber < ActiveRecord::Base
As you can see, I need to note the serial numbers for the products in
stock. For a certain product, number of quantity in stock could reach
hundreds. Thus, I will definitely not make a UI where user must enter
the serial numbers one by one. Fortunately, the serial numbers are
consecutive but in case that it’s not, user must delimit the serial
numbers with comma. Example will be nice:
Product A with quantity = 15 with serial numbers:
User must type in an text_area:
In my opinion, I must:
- Have a way to place a text_area that IS NOT data bound, then
capture what serial numbers to the user type in.
- Validates the serial numbers (see if the supplied serial numbers
match the quantity) and create a SerialNumber object for each one of
- Feed the SerialNumber objects collection to Batch.serial_numbers
- Save the Batch object. Rails will be smart enough to add those
serial numbers to serial_numbers table.
Am I right?
If I am, then how to add a general (not data bound) text area to the
“new” action of Batch controller? How to access the value that is
typed-in by user, how do I validate the presence of serial numbers in
the text area? I suppose I can add a column to table batches (for
decoy) but I don’t want to.
I have tried to add an attribute to Batch (ActiveRecord::Base) object
just to fool text_area. However my attempt failed.
Please advice. Thanks a lot.