Find from any table

hello frnds

Suppose i have generated pages using scaffolding so in the list.rhtml it
is creating something like <% for abc in @abc %> so here when i put an
if condition it will search from the abc db only but if suppose i want
to search from another table then what should I do. can i write simple
sql query which will fetch info from other table of the same db? or is
there ne other way to do so…pls reply.


dhaval parikh


If you have users and posts

You can User.find(blah blah) and Post.find(blah blah) - in both cases
relevant tables will be searched.
If you want to return users that have made posts in the last 10 minutes
(assuming posts have user_id)

User.find(:all, :conditions => [‘posts.created_at > ?’,10.minutes.ago],
:include => 'posts])

You will only get the users who posted in the last 10 minutes.

If you have @abc and you want to search the table associated with the
records in @abc you can (assuming @abc is a collection of activerecord
objects) do blah)

What this will do is get the class, from one of the elements in the
collection. If @abc was a collection of users then @ will be User. Before the
constantize it will be “User”.

Anyway, thats about all I can say and probably all I should say - not
an expert here :slight_smile: