Active record Query

In my controller i am doing these querys.

Query 1

astock = MyStock.find(:first,:select => "Symbol", :conditions
=>["user_id = ?", session[:idnum]])

im wanting to find all the rows from my stock with that particular id.
this works fine.

i then do a second query

Query 2

@stocks_load = Stocks.find(:all, :conditions => ["Symbol =
?",astock.Symbol] )

this also works fine unless the first query is find(:all then i get
symbol as a undefined method error.

Do i need some sort of loop that does the query comparsion in row found
from the first query?

Any help or suggestions would be great!

Regards

Nick

In my controller i am doing these querys.

Query 1

astock = MyStock.find(:first,:select => “Symbol”, :conditions

=>[“user_id = ?”, session[:idnum]])

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

i then do a second query

Query 2

@stocks_load = Stocks.find(:all, :conditions => ["Symbol =

?",astock.Symbol] )

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

is this possible?

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.

Colin Law wrote:

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 …

Colin Law wrote:

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

Is there any relationship between the Stocks table and the MyStocks table?

Colin Law wrote:

Is there any relationship between the Stocks table and the MyStocks
table?

yeah user_id in mystocks relates to the id in a table called users

Sorry, I have lost track of the tables. Is there a Stocks table and a Mystocks table and a Users table, or just a Stocks table and a Users 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.