Topic: How would I do this?...

Alright. I am having trouble determining how I am going to accomplish something.

The software I am writing is to keep track of jobs taking down and setting up garden centers around North America. In addition to keeping track of the jobs, I also have to keep track of which items are at which job. Ie, which components, buildings, units etc.

I have it structured somewhat like this:

Database Tables:

jobs {id, location_id, comments }
locations {id, companyname, storenumber, addressinfo etc}
crewmembers {id, name}
items {id, quantity, job_id, model_id, colour_id, numshelves}
models {id, category_id, part_no, description }
categories {id, title}
colours {id, title}
crewmembers_jobs {crewmwmber_id, job_id}

The relationships are all what you would expect.

A job has_one location, belongs_to_and_has_many crewmembers, has_many items
A crewmember belongs_to_and_has_many jobs
A model has_many items
A category has_many models
A colour has_many items

Now, one thing I cannot figure out a good way of doing is when I create a new job, I am ok with creating the locaiton on a seperate screen, but is there a nice, streamlined way to add items to a job? A job can have up to 100 different items, so I want to do it in a way where when creating a new job, I have a table which contains items belonging to the job, where the process of adding one would be to select a quantity, colour and number of shelves etc. for the item.

Hmmm... after reading that, it looks as clear as mud, but I don't know how to phrase it clearer. Pretty much I am looking for a way to create multiple active records from the same page on the fly. Does that make more sense?

Any help would be much appreciated. I like Ruby so far,, but I'm still learning...

Check out my Blog.

Re: How would I do this?...

You could have a table listing the current items, and a form at the bottom of the table to add an item.  The form could submit through AJAX and dynamically add the new record to the table without refreshing the page.

Does that make sense?

Re: How would I do this?...

Hmmmm... I suspected that the answer would be 'use AJAX'... I guess I'll just have to learn AJAX, then.

Any suggestions on a quick tutorial to learn AJAX with rails?

Check out my Blog.

Re: How would I do this?...

Try this one from Curt Hibbs

vinnie - rails forum admin

Re: How would I do this?...

lol - I just found that one and printed it out. Thanks for the pointer, though. I should check if pragmattic has a good book on the subject...

Check out my Blog.

Re: How would I do this?...

I don't think AJAX will solve this issue. The problem is you are attempting to create items while you are creating a job, so the items don't have any job_id to relate to. If the job is not saved because of validation errors, you need to redisplay the form with all of the un-saved items. I don't see any easy solution to this problem.

You can try something like this to name the items.

<% 1.upto(5) do |index| %>
  Quantity: <%= text_field "item_#{index}", 'quantity' %>
  <!-- etc. -->
<% end %>

Then in your create action you can do a similar loop and grab each item's field and save it if the job was saved successfully. But what if an item has some validation errors and can't be saved? How do you redisplay the form where the job and some items are saved and some aren't? The whole thing can get very complicated. I'd suggest creating a separate page for adding items, in that case AJAX would be an excellent solution because the job_id is established.

Railscasts - Free Ruby on Rails Screencasts

Re: How would I do this?...

ryanb - your idea makes sense; to get it straight, you would suggest making it a two step process - 1 page to create the job, and then a page to add the associated items, passing the jobid to the item-adding page to associate with the items as I add them.

Alrighty - that will work.

Thanks alot - you've really helped alot!

Check out my Blog.