# Formulas in mysql database for rails app

**URL:** <https://www.ruby-forum.com/t/formulas-in-mysql-database-for-rails-app/162211>\
**Category:** Rails\
**Created:** [March 6, 2009, 9:10pm UTC](https://www.ruby-forum.com/t/formulas-in-mysql-database-for-rails-app/162211 "2009-03-06T21:10:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![eyal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/76d3ee/32.png) [@eyal](https://www.ruby-forum.com/u/eyal)\
**Post date:** [March 6, 2009, 9:10pm UTC](https://www.ruby-forum.com/t/formulas-in-mysql-database-for-rails-app/162211/1 "2009-03-06T21:10:46Z")

</div>

Hello,

any ideas how I might store a formula in a mysql database and access  
it through my rails application. For example, say I have a model  
Formula, which has two integer variables formula.var1 and  
formula.var2. And each formula should have a stored formula that is  
made up of only addition and multiplication by numbers and these  
variables.

I want to be able to assign formula.expression=something and  
formula.evaluate=expression evaluated at current variable  
assignments.

Any idea what the best way to implement this might be. I am using  
MySQL 4.1.25 and Rails 2.2.2.

Thanks,  
Eyal

---

<div class="post-metadata">

**Author:** ![eyal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/76d3ee/32.png) [@eyal](https://www.ruby-forum.com/u/eyal)\
**Post date:** [March 6, 2009, 9:57pm UTC](https://www.ruby-forum.com/t/formulas-in-mysql-database-for-rails-app/162211/2 "2009-03-06T21:57:52Z")

</div>

From how I understand you, I think a varchar in the db storing the  
expression using some sort of placeholders for the variables, then your  
evaluate method would sub the vars in and use eval to evaluate the  
subbed text string. You could even use erb to do the substitution if  
need be. Just be careful eval’ing anything that includes user supplied  
variables or expressions. Make sure you thoroughly check the variables  
they supply as they could supply virtually any ruby code to be  
evaluated.

HTH,  
-Bill

eyal wrote:

> formula.evaluate=expression evaluated at current variable  
> assignments.
> 
> Any idea what the best way to implement this might be. I am using  
> MySQL 4.1.25 and Rails 2.2.2.
> 
> Thanks,  
> Eyal
> 
> >

–  
“She was attempting to bicycle kick me when she raised her butt up and  
farted on me your honor.”  
“I have a list of all the dvd’s she had tooken…”  
“I called him because I was mad that he was spreadin my famly business  
round the trailer court…”  
“She said she had no money for rent but she was makin money strippin”  
“…they just completely at the point being turmoiled my life.”  
“…when the motor blowed up 2 months ago.”  
“I basically furnitured the whole house.”  
“I started the conversate on MySpace.”  
“I axed her what a lace from wig was and she axed me to come in for a  
consertation…”  
“He is the emotionalist man I eva seen…”  
“…it literally ranned over my car. I mean, it may be a 2002 DaeWoo,  
but it’s ma ride.”  
“…and here is a picture of your private parts.  
That’s when he stoled my phone.”

- Anonymous Quotes from Daytime Judge Shows
