validkeys | 23 Jul 04:52

Validation method vs. After Save & Rollback


Hey Guys,

Again, I am just learning Rails, so I am asking advice here.

I have a model called Company which has many Owners. The Owners model
indicates the name of the owner and their share (as a percentage) of
the company. The total shares for any one company can not be more than
100%.

So if I have 10 owners each with 10% of the company and I update one
of the owners and set their share to 11%, I should be given a warning
message.

This is what I have done in my Owner model:

def more_than_100_given_away?
  # => find the total share
  company = Company.find(company_id)
  company_total_share = company.total_share  # => This is summing the
old value
  if changed?
    if changes["share"]  # => Changes to a model are stored in the
changes array on the instance, the array has 2 value ["from_value",
"to_value"]
      old_value = changes["share"][0].to_f  # => Convert To Float
      new_value = changes["share"][1].to_f
      new_total_share = company_total_share - old_value + new_value
      new_total_share > 100.01
    else
(Continue reading)

Philip Hallstrom | 23 Jul 20:03

Re: Validation method vs. After Save & Rollback


On Jul 22, 2008, at 7:53 PM, validkeys wrote:

>
> Hey Guys,
>
> Again, I am just learning Rails, so I am asking advice here.
>
> I have a model called Company which has many Owners. The Owners model
> indicates the name of the owner and their share (as a percentage) of
> the company. The total shares for any one company can not be more than
> 100%.
>
> So if I have 10 owners each with 10% of the company and I update one
> of the owners and set their share to 11%, I should be given a warning
> message.
>
> This is what I have done in my Owner model:
>
> def more_than_100_given_away?
>  # => find the total share
>  company = Company.find(company_id)
>  company_total_share = company.total_share  # => This is summing the
> old value
>  if changed?
>    if changes["share"]  # => Changes to a model are stored in the
> changes array on the instance, the array has 2 value ["from_value",
> "to_value"]
>      old_value = changes["share"][0].to_f  # => Convert To Float
>      new_value = changes["share"][1].to_f
(Continue reading)

validkeys | 23 Jul 22:57

Re: Validation method vs. After Save & Rollback


Thanks Philip, would you mind clarifying the wrapping in a
transaction? Im just not quite sure what the means/entails

On Jul 23, 2:03 pm, Philip Hallstrom <phi...@...> wrote:
> On Jul 22, 2008, at 7:53 PM,validkeyswrote:
>
>
>
>
>
> > Hey Guys,
>
> > Again, I am just learning Rails, so I am asking advice here.
>
> > I have a model called Company which has many Owners. The Owners model
> > indicates the name of the owner and their share (as a percentage) of
> > the company. The total shares for any one company can not be more than
> > 100%.
>
> > So if I have 10 owners each with 10% of the company and I update one
> > of the owners and set their share to 11%, I should be given a warning
> > message.
>
> > This is what I have done in my Owner model:
>
> > def more_than_100_given_away?
> >  # => find the total share
> >  company = Company.find(company_id)
> >  company_total_share = company.total_share  # => This is summing the
(Continue reading)

Philip Hallstrom | 24 Jul 19:04

Re: Validation method vs. After Save & Rollback


> Thanks Philip, would you mind clarifying the wrapping in a
> transaction? Im just not quite sure what the means/entails

You want to ensure that that piece of code can only run at once at a  
time.  Transaction isn't probably the best word for it.  Synchronized  
lock, mutex, etc.  Google around for those terms.

> On Jul 23, 2:03 pm, Philip Hallstrom <phi...@...> wrote:
>> On Jul 22, 2008, at 7:53 PM,validkeyswrote:
>>
>>
>>
>>
>>
>>> Hey Guys,
>>
>>> Again, I am just learning Rails, so I am asking advice here.
>>
>>> I have a model called Company which has many Owners. The Owners  
>>> model
>>> indicates the name of the owner and their share (as a percentage) of
>>> the company. The total shares for any one company can not be more  
>>> than
>>> 100%.
>>
>>> So if I have 10 owners each with 10% of the company and I update one
>>> of the owners and set their share to 11%, I should be given a  
>>> warning
>>> message.
(Continue reading)


Gmane