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 8001999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:14:35+00:00 2026-06-04T16:14:35+00:00

I have a accepts_nested_attributes_for relationship between timesheet and timesheetlines. This works great when editing

  • 0

I have a “accepts_nested_attributes_for” relationship between “timesheet” and “timesheetlines”. This works great when editing (edit/update) a timesheet, but when adding a new timesheet (new/create), it returns the following error:

tms timesheetlines tms timesheet can't be empty

It seems it doesn’t know to which timesheet the new timesheetlines belong. This is the relationship in timesheet:

has_many :tms_timesheetlines, :dependent => :destroy, :order=>"daynr ASC"
accepts_nested_attributes_for :tms_timesheetlines, :reject_if => lambda { |a| a[:daynr].blank? }, :allow_destroy => true

And in the “new” action the timesheetlines are build:

@timesheet = TmsTimesheet.new
month_lines = Time.days_in_month(@current_period.period_nr).to_i
month_lines.times { @timesheet.tms_timesheetlines.build }

Any ideas why it works without any problem when editing, but not when creating? Thanks!

Update:

Both saving a new and edited timesheet works when I add this hidden field with each timesheetline when it is a new timesheet:

<%= tl.hidden_field :tms_timesheet_id, :value => timesheet %>

And this when it is an edit:

<%= tl.hidden_field :tms_timesheet_id, :value => timesheet.id %>

Why the difference to make both work?

  • 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-04T16:14:36+00:00Added an answer on June 4, 2026 at 4:14 pm

    Check your validation on :tms_timesheetlines. My guess is that it’s validating the presence of timesheet and failing on that.

    If this is the case, then the reason why it’s failing on create but passing on update is because with nested forms, the parent’s id will not be known (exist) during validation of the child (timesheetlines) but will exist when you come to update the record.

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

Sidebar

Related Questions

I have a accepts_nested_attributes_for association between timesheet and timesheetlines. This works great, but when
I'm using Rails' accepts_nested_attributes_for method with great success, but how can I have it
I'm using Rails' accepts_nested_attributes_for method with great success, but how can I have it
I have a one to many relationship between Admins and Users. Admin has_many :users
I have this Pin Model: class Pin < ActiveRecord::Base belongs_to :user belongs_to :image accepts_nested_attributes_for
I have a nested form for has_many relationship using accepts_nested_attributes_for method. If I don't
i have three models, all for a has_many :through relationship. They look like this:
Say I have two models, Book and Author with a has_and_belongs_to_many relationship between them.
I have a model like this: class Search include Mongoid::Document embeds_many :terms accepts_nested_attributes_for :terms
Say you have this structure: class House < ActiveRecord::Base has_many :rooms accepts_nested_attributes_for :rooms attr_accessible

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.