Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After countless tries, I did something that worked. However, I am still very puzzled, and if someone can explain it that would really help. The only change I made for it to work was saved the <code>post</code> <em>explicitly</em> if <code>user.save()</code> was <code>true</code>. Here is the modified <code>addPost()</code> (the only line changed is marked clearly with <code>// save the post explicitly!!</code>, and there is no other change anywhere else).</p> <pre><code>def addPost() { def user = User.findByLoginId(params.id) if (user) { def post = new Post(params) user.addToPosts(post) if (user.save()) { post.save(flush: true, failOnError: true) // save the post explicitly!! flash.message = "Successfully created Post" } else { flash.message = "Invalid or empty post" } } else { flash.message = "Invalid User Id" } redirect(action: 'timeline', id: params.id) } </code></pre> <p>One note: Even if I did <code>if (user.save(flush: true))</code> instead of just <code>if (user.save())</code> but did not include the explicit save for the post as <code>post.save(flush: true, failOnError: true)</code>, it did not work. I <em>had</em> to save the post explicitly.</p> <p>Since saving the <code>user</code> should have saved the <code>post</code> automatically, this behavior still puzzles me. If someone can explain this behavior it would be very helpful. Thanks to those who took the time to look into this.</p> <p><br> <br> <b>UPDATE --</b> <b>Explanation from Peter Ledbrook below (<a href="https://github.com/GrailsInAction/graina2/issues/64" rel="nofollow">link here</a>):</b></p> <blockquote> <p>Which version of Grails are you using? I just tried with Grails 2.2.1 and got a NullPointerException in that test. Upgrading to 2.2.4 fixed that particular problem. Could be a Grails issue.</p> <p>FYI, you shouldn't even need to explicitly save the User instance in the action to persist the Post. So something is definitely wrong. The cascading save is broken, probably in Grails' mock database implementation that's used during unit tests. I'm guessing that the application works fine when run normally (via run-app for example).</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload