im wanting to find all the rows from my stock with that particular id.
this works fine.
I think this will only find the first one, not all
this also works fine unless the first query is find(:all then i get
symbol as a undefined method error.
If you mean that it does not work if you do query 2 first, it may be
because
astock has not been found yet. If you mean it does not work if the
query 1
is find(:all,…) then astock will be a collection so astock.Symbol is
not
valid. astock[0].Symbol might be, for example.
If you mean that it does not work if you do query 2 first, it may be
because
astock has not been found yet. If you mean it does not work if the
query 1
is find(:all,…) then astock will be a collection so astock.Symbol is
not
valid. astock[0].Symbol might be, for example.
thanks for quick reply
Yeah astock[0].symbol does work. I need it to compare the collection. so
each row in the stored array
In the first query i am getting all the symbols for stocks where the
user id is for example 1 in table mystocks
now i have all the symbols that user id 1 has
then i am doing a second query on another table called stocks to compare
the symbols found in the first query to the symbols in stocks then
outpuuting the result
Forget about how you have done it so far for the moment. Is it possible
to
write out in one sentence what you want you want @stocks_load to
contain?
Find all the stocks where …
Forget about how you have done it so far for the moment. Is it possible
to
write out in one sentence what you want you want @stocks_load to
contain?
Find all the stocks where …
My stocks table which stores the stocks a particular user has, stocks
table which contains the latest stock data and a users table which
contains the user id etc. how ever in my querys i dont involve the users
table as i just pull the user id from the session
I know it is not the question you want the answer to but I feel there is
something not right about the database organisation. Warning bells ring
when
you say that you have stocks in two tables. If changing the database
organisation is an option then I would suggest considering whether you
would
be better to have a ‘has and belongs to many’ relationship between Users
and
Stocks. If the only thing in Mystocks is the symbol., or other
information
that is also in the Stocks table then that table becomes a UserStocks
table
containing only the ids in the conventional HABTM manner. If there is
more
than just the symbol then the UserStocks table can contain the extra
fields.
The advantage will hopefully be that rails, knowing about the
relationships
between the tables, will be able to do more for you. For example to
access
the stocks belonging to a user you just have to use user.stocks rather
than
the find in your original query 1.
I know it is not the question you want the answer to but I feel there is
something not right about the database organisation. Warning bells ring
when
you say that you have stocks in two tables.
Bells of all sorts…
Users may have multiple “lots” of the same stock purchased at different
times and prices, which affects the decision to hold or sell specific
“lots”…
User
has_many lots
Lot
belongs_to user
belongs_to stock
simple user_id, stock_id is insufficient? may also want to include