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
You can use astock.each or other methods to iterate the collection, but
I
think maybe I do not understand what you are trying to achieve.
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 .....
find all stocks from stocks table where user id 1 has them stocks in
there mystocks table
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.