views:

74

answers:

3

A rails app I'm working on features examples of quadratic equations. Obviously, these are all of a common structure: ax^2 + bx + c = 0.

I don't want to store every single example of these. I'd rather generate them from a template. Storing hundreds of possible versions of this structure seems highly wasteful and un-DRY.

On the other hand, if I generate them, I can't access them again reliably as I could if they were simply multiple database objects.

I'm sure there must be a way to overcome this, but I'm still learning rails and I'm obviously not grasping something here. Thanks.

A: 

You have to store the coefficients a, b, c to define your quadratic equations if your examples are arbitrary and independent.

If you want unique quadratic equations, you can throw out ones for which a, b, and c for one equation differ from those of another equation by a constant multiple.

You can generate 1,210 different quadratic equations by choosing a, b, and c to be integers between -5 and +5, inclusive - a can't be zero, of course - but I don't know if that's what you want?

John at CashCommons
+1  A: 

The answer is "it depends". If you're only ever going to do examples of quadratic equations, then you can just store a, b and c. If you think you're going to do other types of equations, store the whole thing.

What really shouldn't be a consideration is that its a "waste" to store the full equation in the sense that you're wasting disk space. Unless you're storing millions of these things, don't worry about it. Disk is cheap.

Other viewpoints to take are YAGNI (Ya Ain't Gonna Need It) which would say to code with the situation you have now, quadratic equations, and don't worry about generalizing it. If you need to you can refactor the code and the data later.

Another way to look at it is KISS (Keep It Simple, Stupid). The simplest thing to do would likely to be to just store the complete equation. This makes retrieving the equation a simple database fetch, no code to generate the equation is necessary.

Personally, assuming you're not doing a huge amount of these things, I'd favor KISS in this situation. I wouldn't be confident that the system is only going to be used for quadratic equations. What you can do is make the system handle whatever equations but make the input form take a, b and c and turn that into a quadratic equation. If you need other equation types later, changing the input logic is much simpler than changing the data structure.

Schwern
There definitely will be other types of equations, some structurally groupable/generable like quadratics, others not. By 'waste' I meant not disk space but repeating myself, so for instance if I wanted to change the wording on each quadratic example I would have to change it in hundreds of places. I'm guessing I can do this with a migration or other mass database manipulation, but I wondered if it was bad practice to store so many more or less identical things. Especially since I'll then be doing the same with other types of equations that are also more or less identical.
Chris
Reading around a bit more, I'm wondering if seeding the db is the way to go here. Presumably I can seed according to a pattern where a pattern is possible, and I can alter this by re-seeding if necessary?
Chris
@Chris Your first comment implies that you're talking about more than just storing the equation but the surrounding example text as well? And its all the same? In that case I'd write a template for the quadratic example which takes key/value pairs as arguments so you can feed it a=2 b=4 c=7 and whatever other variables you find you need later. Other types can use other templates. Then you can just alter the template. This is essentially your original idea of generating the equation, but it makes more sense when its more than just an equation.
Schwern
+1  A: 

Resources don't have to be stored in the database to be restful and reliably accessible. You just need a one-to-one correspondence between the identifier of the resource and the resource you generate.

Just use a,b,c as the identifier of ax^2 + bx + c = 0. Then if your route is resources :quadratics you can generate the url like quadratic_url([a,b,c].join(',')) and in your show method of the QuadraticsController, generate it by doing generate_quadratic(params[:id].split(',').map(&:to_i)).

mckeed