Sunday, September 30, 2007

Grails (0.6) Gotchas...

  • Multi-select dropdown box with size attribute doesn't work as expected

  • Though the documentation select tag in GSP tablibrary doesn't explicitely say anything about multiple select, it works by passing additional parameters ( multiple="multiple" size="4").
    e.g.

    <g:select name="stockPurchase.id" from="${StockPurchase.list()}" optionkey="id" multiple="multiple" size="4"></g:select>


    However, the result will produce a multi select drop-down but the size attribute is not honored. The bug lies in css. Open main.css under web-app/css and edit the following line and remove select,:

    input, select, textarea {


  • many-to-many stores relations in wrong columns

  • e.g.
    Problem domain
    A stock is sold that was purchased multiple times (in fractions).
    A stock is sold multiple times (in fractions) that was purchased in whole once.

    Domain Classes

    class StockPurchase {
    static belongsTo = [StockSale]
    static hasMany = [stockSales:StockSale]
    }

    class StockSale {
    static hasMany = [stockPurchases:StockPurchase]
    }

    Controller

    class StockSaleController {
    //code omitted for brevity
    //...
    def save = {
    def stockSale = new StockSale()
    stockSale.properties = params

    def stockPurchases = params.'stockPurchase.id'
    stockPurchases.each {
    stockSale.addToStockPurchases(StockPurchase.findById(it))
    }

    if(stockSale.save()) {
    flash.message = "StockSale ${stockSale.id} created."
    redirect(action:show,id:stockSale.id)
    }
    else {
    render(view:'create',model:[stockSale:stockSale])
    }
    }
    }


    In addition to stock_sale and stock_purchase tables, it also craetes a reference table named stock_sale_stock_purchase (with columns stock_sales_id, stock_purchases_id) automatically to support many-to-many relationship.

    However, when a stock sale is saved, the reference table ends up with ids switched. But the application works correctly as expected.

    I initially thought there could be a bug. But looks like there isn't and it is expected to work that way.

    Check for an explanation by Graeme Rocher here


No comments:

Post a Comment