Active Records Objects Going Stale

6 views
Skip to first unread message

jason

unread,
Oct 4, 2009, 5:50:30 PM10/4/09
to Ruleby
Dear Groupies,


situation:
=======
I am using active_record objects as facts.

complication
==========
They go stale after changes made to them in the rulebook.
And they go stale for my Rspec tests.

Solutions
=======
reload the object after it has been manipulated.
reload the object before testing the conditions in rspec.

Question
=======
does anyone know why or what needs to be done to change that
behaviour?
it seems a little uncanny that this happening.
maybe someone has some experience about why and when exactly they go
stale.

thanks a million for any feedback.

jason.

Bob Mattax

unread,
Oct 4, 2009, 6:28:06 PM10/4/09
to rul...@googlegroups.com
Isn't that what the modify method is for? Is that what you are
calling "reloading"?

I may not be understanding your question.

bob

jason

unread,
Oct 4, 2009, 8:01:16 PM10/4/09
to Ruleby
Hi Bob,

thanks for the reply.

as far as i can see modify just retracts an asserts an object to the
facts - which in turn triggers the pattern matching.

the active record objects i am using have associations. these
associations get changed in the rulebook.

looking at the objects (that have associations) after the matching
reflects the state before the matching.

in order to obtain the changes made in the rulebook in need to use the
reload method on the object in question to fresh it from the database
and reflect its correct state.

i was wondering if this is the normal behavior?

jason




On Oct 5, 12:28 am, Bob Mattax <rlmat...@gmail.com> wrote:
> Isn't that what the modify method is for?  Is that what you are
> calling "reloading"?
>
> I may not be understanding your question.
>
> bob
>

jason

unread,
Oct 5, 2009, 9:17:21 AM10/5/09
to Ruleby
Just to finish this thread,

the problem is not within ruleby.

it is caused when changing associations on active record objects in
the rule book.

as soon as you move the associations children around the parent goes
stale.
thus, if active record associated objects are part of the facts that
get modified in the rule book, then those changes might not be picked
up by other rules in the way you would expect - which can of course
cause an infinate loop.

therefore reloading the parent of the association in the rule fixes
this behaviour.
to be on the safe side it is probably better to retract the object and
assert it with a find as such:

retract v[:parent_object]
assert Class.find(v[:parent_object])

This obviously results in some additional cost on the database side of
things - so I guess you have to ask yourself if it is better to load
the associations separately in to to the facts and use the rules
conditions to find them. I have opted to write use the association
parents only in the facts and reload them after manipulating them in
the rule book.

Jason.
Reply all
Reply to author
Forward
0 new messages