Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8331333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:23:14+00:00 2026-06-09T02:23:14+00:00

I don’t know the best way to label this but basically, I have 3

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:23:17+00:00Added an answer on June 9, 2026 at 2:23 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know if this has been answered before. Have custom routes to users. If
Don't know why but I can't find a solution to this. I have 3
don't know better title for this, but here's my code. I have class user
Don't know how to google for such, but is there a way to query
I don't know why, but this code worked for me a month ago... maybe
Don't know if I worded the question right, but basically what I want to
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this has been asked before, so point me to another question
I don't know if this question is trivial or not. But after a couple
Don't know if this is the right place to ask this, but I will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.