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.

thanks

dhaval parikh

Hi

If you have users and posts

You can User.find(blah blah) and Post.find(blah blah) - in both cases
the
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

@abc.first.class.name.classify.constantize.find(blah 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 @
abc.first.class.name.classify.constantize will be User. Before the
constantize it will be “User”.

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

regards
ivor