# Newbie: using find like a sql join query

**URL:** <https://www.ruby-forum.com/t/newbie-using-find-like-a-sql-join-query/54672>\
**Category:** Rails\
**Created:** [March 12, 2006, 3:11am UTC](https://www.ruby-forum.com/t/newbie-using-find-like-a-sql-join-query/54672 "2006-03-12T03:11:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [March 12, 2006, 3:11am UTC](https://www.ruby-forum.com/t/newbie-using-find-like-a-sql-join-query/54672/1 "2006-03-12T03:11:21Z")

</div>

I have a working Rails app with several related tables, but can’t find  
an answer to this question in the Dave T. Rails book. Imagine for  
example:

table departments with columns: id, department\_name  
table employees with columns: id, department\_id, employee\_name

and of course the employees table has a constraint foreign key  
(department\_id) references departments(id)

So this is really basic stuff, I have this working fine in my app. I  
have in the model: departments has\_many :employees, and employee  
belongs\_to :department

What I want to do is use the find method to do a query like: "find all  
the employees that belong to the department with department\_name =  
‘Engineering’.

How do I do this with find?

Thanks!  
G

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [March 12, 2006, 6:53am UTC](https://www.ruby-forum.com/t/newbie-using-find-like-a-sql-join-query/54672/2 "2006-03-12T06:53:31Z")

</div>

Hi,

If everything is set up ok, then you can get find department object,  
then get the array of employee objects from the department object.

engineering = Department.find\_by\_name(“Engineering”)  
engineers = engineering.employees

I had problems in more complex queries though, but I use find\_by\_sql  
instead.

Gaudi Mi wrote:

> So this is really basic stuff, I have this working fine in my app. I  
> G

–  
Sau S.

[http://www.saush.com](http://www.saush.com)  
[http://read.saush.com](http://read.saush.com)  
[http://jaccal.sourceforge.net](http://jaccal.sourceforge.net)

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [March 12, 2006, 4:19pm UTC](https://www.ruby-forum.com/t/newbie-using-find-like-a-sql-join-query/54672/3 "2006-03-12T16:19:16Z")

</div>

← Begin overly-long response to simple problem →   
Hey, I was playing around with the example above, and found, at least in  
my case, that you could shorten that to just one line and it works fine  
as well, as so:

engineers = Department.find\_by\_name(“Engineering”).employees

That should work just as well, let us know. I just tested it with a  
similar setup

complaints = Operators.find\_by\_name(“John Q. Public”).opcomplaints

where opcomplaints belongs\_to operators and operators has\_many  
opcomplaints (hopefully, in the real world, our operators will have  
_few_ complaints, but the boss wants to keep track of it anyway). Worked  
like a charm, in any case. Good luck!

Keep in mind, also, that the find\_by\_whatever methods are dynamically  
generated, so if you have a field in your table that keeps track of  
which employees are going bald, and you call it ‘balding’, then you  
could get your list of employees that are going bald by using:

Employees.find\_by\_balding(“Yes”)

You can even search multiple fields… let’s say you’re keeping track of  
which employees are getting a little big around the midle, and you call  
that field ‘pudgy’. Then you can use:

Employees.find\_by\_balding\_and\_pudgy(“Yes”)

It goes without saying that the first record returned here would, of  
course, be my own. ☹

In any case, I’m sure you get the idea. 🙂 Dynamic Finders are one of  
the coolest Ruby features, I think. And of course, there’s little or  
nothing I could find about them in the official API doc, I had to go  
here to find out about them: [http://www.billkatz.com/agile\_web\_rails](http://www.billkatz.com/agile_web_rails)

Anyway, enjoy your coding!

-Daniel

Chang Sau S. wrote:

> Hi,
> 
> If everything is set up ok, then you can get find department object,  
> then get the array of employee objects from the department object.
> 
> engineering = Department.find\_by\_name(“Engineering”)  
> engineers = engineering.employees

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [March 12, 2006, 7:38pm UTC](https://www.ruby-forum.com/t/newbie-using-find-like-a-sql-join-query/54672/4 "2006-03-12T19:38:44Z")

</div>

Bugfix to my post above, 'cause I’m a doofus:

The examples above will, of course, only find the one record. Everything  
shown above also works with ‘find\_all\_by\_whatever’, so to actually get  
the _list_ of balding, pudgy employees, it would be:

Employees.find\_all\_by\_balding\_and\_pudgy(“Yes”)

In fact, just go look at the ‘Dynamic attribute-based finders’ section  
on this page: [http://railsmanual.org/class/ActiveRecord::Base](http://railsmanual.org/class/ActiveRecord::Base)

It will make you happy. 🙂

-Daniel
