Details
-
Type:
Sub-task
-
Status:
Closed
-
Priority:
Minor
-
Resolution: Won't Fix
-
Affects Version/s: 0.5
-
Fix Version/s: None
-
Component/s: Persistence
-
Labels:None
-
Environment:Any
Description
Given
class Widget {
String name
static hasMany = [knobs:Knob]
}
class Knob {
String name
String color
Widget widget
static belongsTo = Widget
}
The following should be supported:
def widget = Widget.findByName("foo")
def blueknobs = widget?.knobs.findAllByColor("blue")
returning the knobs belonging to widget "foo" that are blue.
Currently, you must invert the query, losing DRY-ness and expressiveness:
def widget = Widget.findByName("foo")
def blueknobs = Knob.findAllByWidgetAndColor(widget, "blue")
I also came around this issues. In my case I wanted to show a paginated list of a one-to-many relationship. Applied to you classes above it would be:
WidgetController {
...
def showKnops = {
def widget = Widget.get(params.id)
if(!widget) {
...
} else {
// I have to do
def knobs = Knobs.findAllByWidget(widget, params)
// I want to do / my suggestion
def knobs = widget.listKnobs(params)
// Or with the suggestion above from Clay
def knobs = widget.knobs.list(params)
return [widgetInstance:widget, knobList:knobs]
}
}
}
I also thinking about that this Controller Method should be part of the default scaffolding. I will create another issue about that.