Hey guys. I'm new-ish to rails development and have hit a bit of a wall. The application I'm working on is a scheduling solution that requires updating a join model, but not in a simple 1:1 sort of way.
The app is laid out as follows:
class Route < ActiveRecord::Base
has_many :markers, :foreign_key => 'source_id'
has_many :schedules
accepts_nested_attributes_for :markers, :allow_destroy => true, :reject_if => proc { |a| a['name'].blank? }
accepts_nested_attributes_for :schedules, :allow_destroy => true, :reject_if => proc { |a| a['name'].blank? }
end
class Schedule < ActiveRecord::Base
has_many :arrivals
has_many :markers, :through => :arrivals
accepts_nested_attributes_for :arrivals, :allow_destroy => true, :reject_if => :all_blank
end
class Marker < ActiveRecord::Base
has_many :arrivals
has_many :schedules, :through => :arrivals
end
class Arrival < ActiveRecord::Base
belongs_to :marker
belongs_to :schedule
end
... so a basic has_many :through ... or so I would think :P
When you create a route, you can create 1..n schedules, and 1..n markers. Editing a schedule should allow you to add 1..n arrival entries for each marker defined in the route. THIS is what's causing me grief.
Through the magic of ascii-art, this is what I want the app to look like:
/views/routes/edit.html.erb (works already)
ROUTE
-----
...
SCHEDULES
---------
[Add]
* Schedule 1 [Edit][Delete]
* Schedule 2 [Edit][Delete]
...
MARKERS
-------
[Add]
* Marker 1 [Edit][Delete]
* Marker 2 [Edit][Delete]
* Marker 3 [Edit][Delete]
* Marker 4 [Edit][Delete]
...
/views/schedules/edit.html.erb
SCHEDULE X
----------
[Add Col.]
Marker 1 [ ] [ ]
Marker 2 [ ] [ ]
Marker 3 [ ] [ ]
Marker 4 [ ] [ ]
[x] [x]
(the [x] should remove a column)
EDIT (09NOV04):
I've removed the incomplete view code I originally had posted, but would like to update the question a bit.
I think part of the confusion here (for myself, and possibly for anyone who might be able to help) is that I haven't explained the relationships properly.
- markers have many arrivals
- schedules have many markers
- routes have many schedules
That's the basics.
Having a form that would update arrivals for a single marker wouldn't be difficult, as that's a basic form. What I'm hoping to do is to provide a form that updates all markers at the same time.
When you click on "Add Entry", it should add a new arrival for each marker that's currently available. Under each "column", there should be a "remove" button, that will remove each arrival for that particular column (so from each marker).
I'm not sure if that clears it up any :P