Forum: Ruby on Rails Form element that is not an AR object attribute

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.
5df55b74e42aa3fed9781d1397c1e5e5?d=identicon&s=25 John Indra (Guest)
on 2006-01-13 14:18
(Received via mailing list)
Fellow Railers,

I don't know how to handle this kind of situation the Rails way, please
help.

DB
----
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));

app/models
-----------------
class Batch < ActiveRecord::Base
  belongs_to :product
  has_many :serial_numbers
end

class SerialNumber < ActiveRecord::Base
  belongs_to :batch
end

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:
5692227750-5692227756,5638998830-5638998835,5638729480-5638729481

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
them
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.
John
This topic is locked and can not be replied to.