Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make selection content an attribute for a Rails model
    primarykey
    data
    text
    <p>I am having a hard time even formulating the question I want to be answered, so here's my situation:</p> <p>I'm trying to make a simple stock market plotter tool using an existing database I populate elsewhere. My app already has a nice and dynamic plotter that works with any database, but it expects data in a certain way. So say my model (database) looks like this:</p> <pre><code>Stock: |___ticker___|___open___|___close___|___date___| | aapl | 100 | 101 | 1/1/11 | | aapl | 101 | 102 | 1/2/11 | | goog | 500 | 450 | 1/1/11 | | goog | 450 | 451 | 1/2/11 | ... </code></pre> <p>My plotter routines work off of class attributes (I think thats the terminology), which correspond to columns in the database. </p> <p>I can select all the data corresponding to <code>'aapl'</code>, and easily plot the <code>open</code> and <code>close</code> versus <code>date</code> since my model has said attributues.</p> <pre><code>@stock = Stock.select_by_ticker('aapl') &gt;&gt; @stock.open #=&gt; 100 ... &gt;&gt; @stock.close #=&gt; 101 ... &gt;&gt; @stock.date #= 1/1/11 ... </code></pre> <p>so the attributes would be</p> <pre><code>{open, close, date} </code></pre> <p>But if I want to compare say the closing price for different stocks, I need attributes pertaining to each stock. So basically I want to end up with a model with ticker names as attributes, each corresponding to that ticker's hunk in the database. Using easy to build scopes, I want something like:</p> <pre><code>@stock = Stock.select_close_by_ticker('aapl','goog') </code></pre> <p>attributes are:</p> <pre><code>{aapl, goog, date} </code></pre> <p>where aapl and goog contain the closing price data for just that ticker. I can run multiple database queries if I need to, for now I just want to be able to sort my data into this form. Also, it must be completely dynamic, so I can't hardcode <code>'aapl'</code>, <code>'goog'</code> and all the millions of other tickers into my model. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload