# Validation

**URL:** <https://www.ruby-forum.com/t/validation/170230>\
**Category:** Rails\
**Created:** [June 14, 2009, 4:10pm UTC](https://www.ruby-forum.com/t/validation/170230 "2009-06-14T16:10:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkgobi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pkgobi](https://www.ruby-forum.com/u/pkgobi)\
**Post date:** [June 14, 2009, 4:10pm UTC](https://www.ruby-forum.com/t/validation/170230/1 "2009-06-14T16:10:33Z")

</div>

Hi,

I have one problem,

Regarding Medical store distributuin

```
   Store have two users i mean two sales persons, they are

```

distributing medicine to patients.

```
   Store having 50 colpol tablets, both users are trying to sale 40

```

colpol tablets at a same time, it means 80 tablets but store having 50  
colpols only.

```
   For this i want reserve the stock while form entry itself,as well

```

as i dont want to use database level.

```
  any ruby or rails concept is there....?

```

advance thanks

---

<div class="post-metadata">

**Author:** ![pkgobi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pkgobi](https://www.ruby-forum.com/u/pkgobi)\
**Post date:** [June 15, 2009, 8:00am UTC](https://www.ruby-forum.com/t/validation/170230/2 "2009-06-15T08:00:15Z")

</div>

actually, if it is the same database, then there is no way to happen at  
the same time, right?

I don’t know, maybe something like this:

class Tablet \< ActiveRecord::Base

validate :must\_have\_in\_stock

def must\_have\_in\_stock  
qnt = Tablet.find(product\_id).quantity  
erros.add(:quantity, ‘There is only ’ + qnt + ’ in stock.’)  
if quantity \> qnt  
end  
end

---

<div class="post-metadata">

**Author:** ![pkgobi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pkgobi](https://www.ruby-forum.com/u/pkgobi)\
**Post date:** [June 15, 2009, 9:05am UTC](https://www.ruby-forum.com/t/validation/170230/3 "2009-06-15T09:05:10Z")

</div>

On Jun 15, 7:00 am, Le Sa [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> actually, if it is the same database, then there is no way to happen at  
> the same time, right?

Unfortunately, not true.

Anything working purely at the ruby level (such as a validation) will  
run into race conditions doing this. One of Rails’ answers to this is  
optimistic locking (described in the active record api docs).

Fred

---

<div class="post-metadata">

**Author:** ![pkgobi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pkgobi](https://www.ruby-forum.com/u/pkgobi)\
**Post date:** [June 15, 2009, 9:28am UTC](https://www.ruby-forum.com/t/validation/170230/4 "2009-06-15T09:28:18Z")

</div>

Frederick C. wrote:

> On Jun 15, 7:00ï¿½am, Le Sa [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > actually, if it is the same database, then there is no way to happen at  
> > the same time, right?
> 
> Unfortunately, not true.
> 
> Anything working purely at the ruby level (such as a validation) will  
> run into race conditions doing this. One of Rails’ answers to this is  
> optimistic locking (described in the active record api docs).
> 
> Fred

Mr. Frederick C. thanks for replying,

As u said, backend i have validated everythig. the person who submits  
first get saved and for the second person it should display stock not  
available…

But my expectation is once the first user has selected the quantity  
then for the second user the stock displayed should be available  
quantity minus first user selected quantity.

note : This should work before both the users save the form.

---

<div class="post-metadata">

**Author:** ![pkgobi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pkgobi](https://www.ruby-forum.com/u/pkgobi)\
**Post date:** [June 15, 2009, 11:06am UTC](https://www.ruby-forum.com/t/validation/170230/5 "2009-06-15T11:06:24Z")

</div>

Frederick C. wrote:

> On Jun 15, 8:28ï¿½am, Gopi G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > ï¿½ As u said, backend i have validated everythig. the person who submits  
> > first get saved and for the second person it should display stock not  
> > available…
> > 
> > ï¿½ But my expectation is once the first user has selected the quantity  
> > then for the second user the stock displayed should be available  
> > quantity minus first user selected quantity.
> > 
> > note : This should work before both the users save the form.
> 
> Then you’re going to have to spend quite a lot of time coming up with  
> some sort of ajaxy notification thing to track all this.
> 
> Fred

yes mr. Fred

for that we need to keep the data commonly for all users , where we can  
keep except database?.

whether we can save it anywhere in our application, that can be  
editable?

---

<div class="post-metadata">

**Author:** ![pkgobi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pkgobi](https://www.ruby-forum.com/u/pkgobi)\
**Post date:** [June 15, 2009, 9:45am UTC](https://www.ruby-forum.com/t/validation/170230/6 "2009-06-15T09:45:58Z")

</div>

On Jun 15, 8:28 am, Gopi G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> As u said, backend i have validated everythig. the person who submits  
> first get saved and for the second person it should display stock not  
> available…
> 
> But my expectation is once the first user has selected the quantity  
> then for the second user the stock displayed should be available  
> quantity minus first user selected quantity.
> 
> note : This should work before both the users save the form.

Then you’re going to have to spend quite a lot of time coming up with  
some sort of ajaxy notification thing to track all this.

Fred

---

<div class="post-metadata">

**Author:** ![pkgobi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pkgobi](https://www.ruby-forum.com/u/pkgobi)\
**Post date:** [June 15, 2009, 11:15am UTC](https://www.ruby-forum.com/t/validation/170230/7 "2009-06-15T11:15:13Z")

</div>

Gopi G. wrote:

> Frederick C. wrote:
> 
> > On Jun 15, 8:28ï¿½am, Gopi G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > wrote:
> > 
> > > ï¿½ As u said, backend i have validated everythig. the person who submits  
> > > first get saved and for the second person it should display stock not  
> > > available…
> > > 
> > > ï¿½ But my expectation is once the first user has selected the quantity  
> > > then for the second user the stock displayed should be available  
> > > quantity minus first user selected quantity.
> > > 
> > > note : This should work before both the users save the form.
> > 
> > Then you’re going to have to spend quite a lot of time coming up with  
> > some sort of ajaxy notification thing to track all this.
> > 
> > Fred
> 
> yes mr. Fred
> 
> for that we need to keep the data commonly for all users , where we can  
> keep except database?.
> 
> whether we can save it anywhere in our application, that can be  
> editable?

once our input get saved, then we will detete that user related data  
from that storage. if we go for data base using ajax it will slow in  
frontend itself.
