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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:01:42+00:00 2026-05-18T08:01:42+00:00

Related objects of an m:n relation can be selected with a html select element

  • 0

Related objects of an m:n relation can be selected with a html select element in my application (could also be a list of checkboxes). If all related objects are deselected in the edit form, the objects list of related objects should be cleared. With scaffold code it works only if at least one related object remains.

Example:

class Book {
  String name
  static hasMany = [authors: Author]
  belongsTo = Author
}

class Author {
  String name
  static hasMany = [books: Book]
}

def b1 = new Book(name: "B1").save()
def b2 = new Book(name: "B2").save()
def author = new Author(name: "Stephen").addToBooks(b1).addToBooks(b2).save()

// How to remove all books from the author?

Following the Grails doc I would call removeFrom* (the opposite of addTo*) on the domain object for all related objects. But to do this, I had to figure out, what objects are related. Is there an easier way to clear the list of related objects? Would the bidirectional association be handled correctly, if the list of related objects would simply be replaced by an empty list?

  • 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-05-18T08:01:43+00:00Added an answer on May 18, 2026 at 8:01 am

    I think the issue that you might be experiencing is that when so item is selected either in the select box or via checkboxes then no data is coming back with the request to the controller. If you want to be able to remove all objects from the association you need to add an extra hidden field to the form.

    For example if there is a product that has many categories add the following:

    <g:hiddenField name="categories" value=""/>
    

    apart from the usual select.

    You can write a test for the controller to make sure that the desired functionality behaves as expected.

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

Sidebar

Related Questions

I want get all of the Geom objects that are related to a certain
I want to filter all Relation Objects where (relation= following relation in a virtual
I have a UserForm class which has a select list populated from a related
I have a query related to relation between objects. Scenario: Payment Details - a.
In my database, I have a number of objects that can be related to
I ran the code to create the generically related objects from this demo: http://www.djangoproject.com/documentation/models/generic_relations/
This question is specifically related to overriding the equals() method for objects with a
This is related to the Nested Database transactions in C# . The objects in
This is related to my previous question , regarding pulling objects from a dmp
Does anyone know how to delete an object and all of it's related entities.

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.