Form element that is not an AR object attribute

Fellow Railers,

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
belongs_to :product
has_many :serial_numbers

class SerialNumber < ActiveRecord::Base
belongs_to :batch

The problem

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:
5692227750-5692227756 (7)
5638998830-5638998835 (6)
5638729480-5638729481 (2)
User must type in an text_area:

In my opinion, I must:

  1. Have a way to place a text_area that IS NOT data bound, then
    capture what serial numbers to the user type in.
  2. Validates the serial numbers (see if the supplied serial numbers
    match the quantity) and create a SerialNumber object for each one of
  3. Feed the SerialNumber objects collection to Batch.serial_numbers
  4. 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs