To understand question 1, keep it simple and pretend the attributes you want to access are in the factories table. The design pattern then is pretty simple:
Database schema:
create_table :factories do |t|
t.boolean :now_operating, :default => true, :null => false
...
Controller:
def edit
@factory = Factory.find(params[:id])
if request.post?
@factory.update_attributes(params[:factory])
end
end
View:
<% form_for :factory, @factory, :url => { :action => "edit" } do |f| %>
<%= f.checkbox :now_operating %>
...
You can do this with less code, in fact with none at all if you use RESTful resources and follow naming conventions, but this is a little too much magic to start off, because it conceals what rails is doing for you.
Namely: when the :get action loads an object matching the object name passed to form_for, rails will populate the form fields with that object's attribute values from the database. Then, when the client submits the form, rails encodes those fields into the request as params for that object, which you can then update in the :post action. See the official Rails Guide for a more detailed treatment.
I apologize if that was a little too basic, but it's important to get the basic pattern because the general approach to solving problem 2 is the same:
- Load the objects in your :get action.
- Render a form with the objects enclosed.
- Save the params for each object.
There are several ways of actually doing this for forms with multiple objects. I suggest you see the various tutorials listed under Item 8, Building Complex Forms, in the Rails Guide.
Two last points to consider:
- Do you really need those extra tables? If each maintenance row
contains attributes for a single
factory, it may make more sense to
just put the attributes in the
factory table and simplify your forms
and actions.
- Attributes such as :maintenance_level might be more
appropriately represented as a
:string type with a radio_button in
the form to set the level.