# Is it possible to temporary cache an attribute's value in memory or do something similar?

**URL:** <https://www.ruby-forum.com/t/is-it-possible-to-temporary-cache-an-attributes-value-in-memory-or-do-something-similar/189510>\
**Category:** Rails\
**Created:** [June 2, 2010, 1:19am UTC](https://www.ruby-forum.com/t/is-it-possible-to-temporary-cache-an-attributes-value-in-memory-or-do-something-similar/189510 "2010-06-02T01:19:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Hillsboroug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@Matthew\_Hillsboroug](https://www.ruby-forum.com/u/Matthew_Hillsboroug)\
**Post date:** [June 2, 2010, 1:19am UTC](https://www.ruby-forum.com/t/is-it-possible-to-temporary-cache-an-attributes-value-in-memory-or-do-something-similar/189510/1 "2010-06-02T01:19:52Z")

</div>

Hello,

I have a bit of code that depicts this hypothetical setup below. A  
class Foo which contains many Bars. Bar belongs to one and only one  
Foo. At some point, Foo can do a finite loop that lapses 2+  
iterations. In that loop, something like the following happens:

```
bar = Bar.find_where_in_use_is_zero
bar.in_use = 1

```

Basically what find\_where\_in\_use\_is\_zero does something like this in  
as far as SQL goes:

`SELECT * from bars WHERE in_use = 0`

Now the problem I’m facing is that I cannot run the following line of  
code after bar.in\_use =1 is invoked:

```
bar.save

```

The reason is clear, I’m still looping and the new Foo hasn’t been  
created, so we don’t have a foo\_id to put into bars.foo\_id. Even if I  
set to allow foo\_id to be NULL, we have a problem where one of the  
bars can fail validation and the existing one was saved to the  
database. In my application, that doesn’t work. The entire request is  
atomic, either all succeeds or fails together. What happens next, is  
that in my loop, I have the potential to select the same exact bar  
that I did on a previous iteration of the loop since the in\_use flag  
will not be set to 1 until @foo.save is called.

Is there anyway to work around this condition and temporarily set the  
in\_use attribute to 1 for subsequent iterations of the loop so that I  
retrieve an available bar instance?

Thanks,

Matthew

---

<div class="post-metadata">

**Author:** ![Matthew\_Hillsboroug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@Matthew\_Hillsboroug](https://www.ruby-forum.com/u/Matthew_Hillsboroug)\
**Post date:** [June 2, 2010, 8:30am UTC](https://www.ruby-forum.com/t/is-it-possible-to-temporary-cache-an-attributes-value-in-memory-or-do-something-similar/189510/2 "2010-06-02T08:30:34Z")

</div>

On Jun 2, 12:18 am, Matthew Hillsborough  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there anyway to work around this condition and temporarily set the  
> in\_use attribute to 1 for subsequent iterations of the loop so that I  
> retrieve an available bar instance?

Well if things do need to be atomic then you probably want to wrap  
this in a transaction and roll back if one of those validations fail.  
You could also change your conditions on that find to exclude a bar  
with a particular id

Fred
