# Find from any table

**URL:** <https://www.ruby-forum.com/t/find-from-any-table/114068>\
**Category:** Rails\
**Created:** [September 7, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/find-from-any-table/114068 "2007-09-07T15:45:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [September 7, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/find-from-any-table/114068/1 "2007-09-07T15:45:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [September 7, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/find-from-any-table/114068/2 "2007-09-07T17:31:09Z")

</div>

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 🙂

regards  
ivor
