I don’t know the best way to label this but basically, I have 3 models,
Course: A subject, which has many Sessions
Session: Has many Slots that make up one session
Slot: Has a date and time which it happens on
The idea is, if you are interested in a Course, you register to take a Session. Each Session will have the same number of Slots, but these could be at different times of the month (e.g. you could take a 5 day session on the 30th July to 3rd Aug, or the same session a week later, 6th Aug to 10th Aug)
So I need to try and set up my nested for, so you can add or remove Sessions, each one should have the same number of nested Slots (based on a number in a drop down somewhere up the form).
Can anyone think of a nice way of doing this? or even a nasty way? I’m having trouble 🙂
Update
I have it working with some JS and stuff, but now when it saves, it always errors saying Sessions slot session is not valid — its not setting the slots session_id to its parent session and its not setting the sessions course_id to the course
I can’t work out why. Anyone seen this before? The objects are in the has_many array…
Update 2
{"title"=>"test123", "description"=>"test123", "sessions_attributes"=>{"0"=>{"slots_attributes"=>{"0"=>{"when(3i)"=>"10", "when(2i)"=>"8", "when(1i)"=>"2012", "when(4i)"=>"10", "when(5i)"=>"00", "_destroy"=>"0"}, "1"=>{"when(3i)"=>"10", "when(2i)"=>"8", "when(1i)"=>"2012", "when(4i)"=>"10", "when(5i)"=>"00", "_destroy"=>"0"}, "2"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}, "3"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}, "4"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}, "5"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}, "6"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}, "7"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}, "8"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}, "9"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}, "10"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}, "11"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}, "12"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}, "13"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}, "14"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}, "15"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}, "16"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}, "17"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}, "18"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}, "19"=>{"when(3i)"=>"", "when(2i)"=>"", "when(1i)"=>"", "when(4i)"=>"", "when(5i)"=>"", "_destroy"=>"1"}}}}}
Update 3
adding this code before saving has fixed it, although I don’t know why I need it.
def apply_nested_models_hack
## HACK ##
@course.sessions.each do |s|
s.course = @course
s.slots.each { |ss| ss.session = s }
end
## /HACK ##
end
I’d like to know why the relationships aren’t getting set, even though I’ve fixed my issue I’ve not answered the question.
Update 4
It turns out this was not the nested forms but the fact I had:
validates :session, presence: true
on Slot – ActiveRecord doesn’t set this side of the relationship until after saving, therefore it was considered invalid and wouldn’t save. I’d consider this a bug in AR as it should show the relationships as they were in the database, even if they haven’t been persisted yet.
It turns out this was not the nested forms but the fact I had:
on Slot – ActiveRecord doesn’t set this side of the relationship until after saving, therefore it was considered invalid and wouldn’t save. I’d consider this a bug in AR as it should show the relationships as they were in the database, even if they haven’t been persisted yet.