I googled couple hours but didn't find the answer. I have an Author who hasMany Book.
Class Author {
String name
static hasMany = [books: Book]
}
The <f:all> tag in the edit.gsp view will display all the properties from the Author and list all the Book and a Add Book button at the bottom of the page.
I want the <f:all> tag to display only the properties from the Author. I don't want it to display the list of books or the Add Book button.
Where and what can I type to set the <f:all> tag from only displaying the actual properties from the Author domain?
Related
I have an Author who has many books. A book has many chapters. The book is published by different publishers who pay different commissions.
So, the domains will be.
Author { hasMany [books: Book] }
Book { hasMany [chapters: Chapter, publishers: Publisher] }
Publisher { }
Grails generates the book_chapter and book_publisher tables. So far so good.
The database looks good.
Commission is a value of the type Single. But where do I put the commission?
Would you show me how to set this up please?
Many Thanks!
I give up. I don't know how to set up the relationship between the Author, Book, and Publisher. The idea is simple. Different publishers will pay me different commissions for my books.
I will remove the hasMany Publisher relationship from the Book. So, my Book is just now:
Author { hasMany [books: Book] }
Book { hasMany [chapters: Chapter] }
Publisher { ... }
Now I will add a Commission domain to have my commission amount stored in the database.
Commission {
Author author
Book book
Publisher publisher
Float amount
}
Next I need to make a composite primary key with both Author, Book, and Publisher. This will prevent duplicated records. It is because this is not correct to have more than one record in the database to show how much the publisher pays me for the same book.
I think this will work. What I need to do next is to program the controller to fill out the commission objects manually.
I originally don't want to think or type too much if I can use the built-in scaffolding. Let Grails do all the heavy lifting for me. I don't think I can use the scaffolding anymore.
Good job me.
Please leave a comment.
class Cart {
static hasMany = [list: Fruit]
}
Say we made a domain in Grails with a hasMany relationship with another model. So in our service, if we want to update the list for an object, is it better to replace the entire list with a new list, or is it better to individually add/remove the items from the list?
Let me preface this ask by indicating that this only applies to /list/ view and NOT other views. This is NOT about drop-down menus
I have two tables:
[Authors]
- id
- name
[Books]
- id
- title
- author
In the Books form (create/edit) I can get the "author" field to display an author's name (from Author table instead of id) without an issue, however, when I view the /list/ view, it displays the author's id instead of the name (correctly, since this is what's stored in the DB).
I have not been able to find a method to convert "id" to "name" in the /list/ view elsewhere and I haven't used grails in almost a year (yay rust). Thanks in advance for guidance.
Try this:
class Author {
String name
static mapping = {
id name: 'name'
}
}
If you're using static scaffolding, don't forget to regenerate the views for the Author and Book domain classes:
grails> generate-views com.yourPackage.Author
grails> generate-views com.yourPackage.Book
Given the following domain classes, it is possible to get the value of author's ID from a Book object without fetching the related Author object like this: Book.get(1).authorId (see this SO question).
class Author {
hasMany = [books: Book]
}
class Book {
belongsTo = [author: Author]
}
However, adding books ignoreNotFound:true to Book static mapping (to get around some legacy issues) causes Book.get(1).authorId to be null.
Is there a way to get the value of AUTHOR_ID column in the BOOK table when the related record is missing in the AUTHOR table?
I am having to two domain classes Order and Stock. When stock is sold I am creating an entry in the child table StockOrder which contains information about the Order(order_id) and Stock(stock_id) and noOfStockSold.
In my current design I coded the StockOrder close to Stock table. You can see this below.
Class Stock {
String stockName
BigDecimal quantity
List stockOrderList
static hasMany = [stockOrderList: StockOrder]
}
class StockOrder {
Stock stock
Order order
BigDecimal noOfStockSold
static belongsTo = [Stock]
}
class Order {
List saleLineItemList
static hasMany = [saleLineItemList: SaleLineitem]
}
Am I doing to correctly from ERP prespective. How to relate Order to Stock sold?
Is it ok if I tie StockOrder to Order also by doing static belongsTo = [Stock,Order]
Is there any better way of doing it or any improvements?
I would start by reading these:
http://grails.org/doc/2.0.x/ref/Domain%20Classes/belongsTo.html
http://grails.org/doc/2.0.x/ref/Domain%20Classes/hasMany.html
Basically you use the belongsTo and hasMany to describe bi-directional relationships. This allows you to cascade delete objects if you so desire. I would imagine in an ERP system that you wouldn't want cascading functionality because if you delete a Stock you probably don't want to delete all the associated StockOrder. I would probably keep the hasMany side of the relationship and remove the belongsTo since you're already associating a StockOrder to a Stock and an Order.