TOPIC: Rule updating that stores to the database
#1467
Rule updating that stores to the database 11 Months ago
I have a rule that computes the cost of a flight based on 3 fields in the current record. The rule works great when initially creating the record, however, once the record is created, it is not updated if one of those three fields change (aircraft, time day, time night). This is my rule function:

Code:


function()
{
  if (.aircraft.Size() > 0 and (Number?(.time_day) or Number?(.time_night)))
  {
    ac = Query1("aircraft where tail_no = '" $ .aircraft $ "'")
    if (Record?(ac) and Number?(ac.rental_fee)) {
      total_time = 0
      if (Number?(.time_day))
        total_time += .time_day
      if (Number?(.time_night))
        total_time += .time_night
      return total_time * ac.rental_fee
    }
  }
}



I was thinking that when .aircraft, .time_day, or .time_night were changed then the rule should recompute. It only does this when creating a new record, not when editing an old record.

Can I force it to recompute when creating or editing?

Jeremy
 
 
Last Edit: 2009/09/21 07:19 By jeremy_c. Reason: Code formatting.
 
#1468
Re:Rule updating that stores to the database 11 Months ago
Oh, I did want it to store to the database as the rental fee of the aircraft may change (go up of course!) in the future and I don't want it to recompute old records with the new rate, thus, I figured I would store the computed rental cost with each log entry.
 
 
#1475
Re:Rule updating that stores to the database 11 Months ago
The reason it doesn't work is that when it reloads the record it doesn't know the dependencies.

I think all you need to do is store the dependencies. You do this by creating a field with "_deps" on the end. e.g. If your field is called "myfield" you need "myfield_deps".

This is briefly mentioned in the help under Database > Rules > Stored Dependencies.

You can also manually set the dependencies using record.SetDeps

Note: You'll need to update any existing data to fill this in.

Note: If you want to see _deps fields from QueryView you need to rename them e.g. mytable rename myfield_deps to myfield_d

Hope that helps.
 
 
andrew
 
#1476
Re:Rule updating that stores to the database 11 Months ago
I read about _deps but I didn't understand what should go into that field?

Jeremy
 
 
#1480
Re:Rule updating that stores to the database 11 Months ago
You don't need to put anything in it, you just need to create it and Suneido will automatically use it.
 
 
andrew
 
#1498
Re:Rule updating that stores to the database 11 Months ago
I cannot seem to make this work. I added l_rental_cost_deps. All computes fine but when I go back to edit the record, the rental cost does not auto compute. I tried this on existing records and also new records that were added after l_rental_cost_deps was added.

Any more thoughts on debugging this?

Thanks,

Jeremy
 
 
#1499
Re:Rule updating that stores to the database 11 Months ago
Hm, I do see that it recomputes on edit as long as t_rental_cost is empty. As soon as it inserts a value into the field, it no longer computes things. It seems that once a value is placed into the computed field the _deps field is blanked, i.e. no dependencies.

Jeremy
 
 
#1503
Re:Rule updating that stores to the database 11 Months ago
Strange. I am not sure why it is not working. We use this in our applications.

Have you checked whether the dependencies are getting saved? i.e. from Query View (renaming the _deps field to make it visible)

NOTE: It only saves dependencies on fields within that table.
 
 
andrew
 
#1506
Re:Rule updating that stores to the database 11 Months ago
yes. For records that do not have a "rental_cost" already computed and stored in the database, the correct dependencies are in the _deps field. However, once it calculates (and saves) the rental_cost, the _deps field goes empty.

I can send you my database if you would like to see?

Jeremy
 
 
#1509
Re:Rule updating that stores to the database 11 Months ago
Sure, I can take a look. (zip it up and send to mckinlay at suneido.com) If you can simplify it to a small example that will make it easier to look at.

It must be something your code is doing. If your stdlib tests are passing then this facility is working (e.g. see RuleTest)
 
 
andrew
 
#1510
Re:Rule updating that stores to the database 11 Months ago
Ok,

I emailed it to you. All rules are passing fine and my code is not doing anything special. I am not saying it's not my fault, but if it is, I am at a loss as to what it would be.

Thanks for looking at this,

Jeremy
 
 
#1511
Re:Rule updating that stores to the database 11 Months ago
Andrew,

I figured it out. My Rule_l_rental_cost depended on L_t_time which was also a rule which depended on L_d_time and L_n_time which were also both rules. Thus, I had to add _deps fields for L_t_time, L_d_time and L_n_time.

L_d_time = total day time (day pilot in command, day second in command + day dual)

L_n_time = total night time (night pilot in command, night second in command + night dual)

L_t_time = total time (day + night)

Once I added _deps field for those three rules the rental cost calculation started working as well.

Thanks for the help,

Jeremy
 
 
#1516
Re:Rule updating that stores to the database 11 Months ago
That's great, glad you figured it out.
 
 
andrew