views:

55

answers:

1

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.

  1. markers have many arrivals
  2. schedules have many markers
  3. 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

+1  A: 

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.

There is nothing linking markers to routes as far as schedules are concerned. The way you've laid things out any schedule can add any number arrival entries for each marker defined in your database.

You need make a few changes to get the functionality you want. I'm assuming that a schedule belongs_to a route. I've also left out the accepts_nested_attributes_for lines out, to conserve space.

class Route < ActiveRecord::Base
  has_many :markers, :foreign_key => 'source_id'  
  has_many :schedules
  ...
end

class Schedule < ActiveRecord::Base  
  has_many :arrivals
  belongs_to :route
  has_many :markers, :through => :arrivals
  has_many :route_markers, :through => :route, :source => :markers
  ...
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

Now @schedule.route_markers returns a list of markers in the route linked to the schedule. You can use those to generate your grid. Then create arrival objects to establish a marker in a specific schedule.

Then it's just a matter of @schedule.markers= list_of_markers and rails takes care of creating/deleting entries in the join table.

Sorry but without knowing more, I'm not going to speculate on what the view will look like.

EmFi
Now that I have route_markers, I can get the markers in my controller by doing a @schedule.route_markers.find(:all), and use that to build the list in the view.This is what I'm trying to do that's messing me up a bit. I'm using the markers list to build a simple table. Each "row" should represent a number of arrival entries for a particular marker. I'm using the "complex-forms" example to setup the dynamic addition of "columns", but I'm not sure how to set that part up for input and editing. I have an "ideal" view configuration in the original post if that helps any.
Maximus
I don't get what your columns are supposed to represent. Is each column supposed to be an arrival? Because the way things are laid out now, there's no way to assocaite multiple arrivals for a marker. Could you update your question with an better explanation of the form?
EmFi
Hey Emfi,What I'm trying to do is this: Each marker can have multiple arrivals. What I want to do though is to be able to add a new "column" of arrivals based on the number of available markers. I think this is where it's getting messy. You shouldn't be able to add "single" arrival entries at a time, though technically that's how it's going to be stored in teh database.The usecase is this: If you have markers called Point1, Point2 ... Pointn, when you click "Add", it should generate fields for EACH marker, and each field represents an arrival.
Maximus
Emfi, I updated the description ... hopefully that makes a bit more sense than my previous comment :P
Maximus