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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:47:25+00:00 2026-06-10T11:47:25+00:00

I have a Task model, which is joined in a many-to-many relationship with an

  • 0

I have a Task model, which is joined in a many-to-many relationship with an Objective model.

I have a Task edit form where a user can associate any Objective with a Task via checkboxes. When a checkbox is checked, it should indicate an association between the Task and a particular Objective; when a checkbox is not checked, there should be no association. This should be persisted to the database when the form submits.

<%= form_for @task do |f| %>
  <% Objective.all.each do |objective| %>
    <%= check_box_tag :objective_ids, objective.id, @task.objectives.include?(objective), :name => 'task[objective_ids][]' %>
  <% end %>
  <%= f.button :submit %>
<% end %>

Updating a Task seems to work absolutely fine as long as one checkbox is checked by the user, but when the user does not check any checkboxes, the :objective_ids param (which is an array of Objective ids) is not included in the POST action at all. Because of this, when I do @task.update_attributes(params[:task]) in the controller, the Task’s collection of Objectives is not updated (i.e. the Task should no longer have any Objectives associated with it, because no checkboxes were checked).

So how can I ensure the :objective_ids param is included in the POST, even if only as an empty array?

  • 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-10T11:47:26+00:00Added an answer on June 10, 2026 at 11:47 am

    Add hidden field above all checkboxes with empty value. It will be sent in case user didn’t check any checkboxes.

    <%= form_for @task do |f| %>
      <%= hidden_field_tag "task[objective_ids][]", nil %>
      <% Objective.all.each do |objective| %>
        <%= check_box_tag :objective_ids, objective.id, @task.objectives.include?(objective), :name => 'task[objective_ids][]' %>
      <% end %>
      <%= f.button :submit %>
    <% end %>
    

    Here is a good railscasts about this.
    You may also want to check the source code for it.

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

Sidebar

Related Questions

I have a model Task which will HABTM many TaskTargets. When it comes to
I have a model User which automatically has a Task generated. I want to
I have an interesting scenario. I have a task model which has a task
I have the fllowing requirement, I have a model called Task to display user
I have a Project model which accepts nested attributes for Task. class Project <
Lately i have got a task to import a 3D model which is created
I have the following model: class Task(Model): solvers = ManyToManyField(User, related_name='slvrs') Now I want
I have a Core Data model in which a Task entity includes an optional
In a Rails 3.2 app I have a model Project, which has many Tasks.
I have data in an external database which can be accessed via JSON. Now

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.