Why is the database not updated before a next Rule is launched? I have two Rules (created with the Rules module) that are both launched when an article is flagged with the Flag Approved:


Rule 1. Publish the flagged article with the action Publish content.
Rule 2. Count the amount of published articles by the author of the flagged article. Display a message when this number is 5.


I'm using the following code in the condition of Rule 2:

return MYMODULE_count_articles($flagged_node-&gt;uid) == 5;


where my custom module contains the function

function MYMODULE_count_articles($uid) {
  $count = db_query("SELECT COUNT(*) FROM node WHERE uid=$uid AND status=1 AND type='article'")-&gt;fetchField();
  return $count;
}


The condition of Rule 2 is evaluated as FALSE when the 5th article is flagged with Approved, but evaluates as TRUE when a 6th article is flagged.

I suspect that this problem is caused because the database hasn't been updated yet when Rule 2 launches, because debugging the $count variable at the 5th flagging returns 4 instead of 5. Also, executing the condition's code afterwards yields the correct value 5.

Is this a know problem/phenomenon or a bug in the Rules module? Or is there an easy way to fix it (apart from changing the condition to ... == 4, which is really dirty)?
Have a look at issue # 430274, about an event which is a variation of your event here, and somehow explains that "after saving something" actually happens ... "before saving something" (which is not a bug, simply how the Rules module  works ...).

A possible compromise to avoid these kind of issues, is to transform what you're trying to do in your Rule 2 in a Rules Component, so that your Rule 1 "schedules the execution" (using the Rules Scheduler submodule) of that Rules Component. For example after only a few seconds or minutes later on (if your cron job runs frequent enough, otherwise it'll be next time cron runs). This compromise will ensure that, at the time the Rules Component is executed, the "Publish content" for sure completed, and your "number" will have been updated as expected.