# Sql query confusion

**URL:** <https://discuss.rubyonrails.org/t/sql-query-confusion/48953>\
**Category:** rubyonrails-talk\
**Created:** [January 29, 2010, 8:09am UTC](https://discuss.rubyonrails.org/t/sql-query-confusion/48953 "2010-01-29T08:09:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 29, 2010, 8:09am UTC](https://discuss.rubyonrails.org/t/sql-query-confusion/48953/1 "2010-01-29T08:09:14Z")

</div>

Hello champs,

Scenario is that i have a table called "employee" and a column for manager\_id inside it.. and another table called "leaves" and having a column employee\_id inside it.. I am copying here one record..

Employee table- id employee\_name manager\_id created\_at .. 1 hemant 1 whatever 2 hemant 1 whatever

Leave table- id employee\_id status created\_at .. 1 1 pending whatever 2 2 granted whatever

Now i want to find out all the leave records of an employee whose manager id is session id.. like i logged in using employee\_id = 1, then is should show me all leaves of employee whose manager id = 1..

Hope you people got me right, may be a one liner only .. 🙂

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 29, 2010, 1:05pm UTC](https://discuss.rubyonrails.org/t/sql-query-confusion/48953/2 "2010-01-29T13:05:17Z")

</div>

Something like Leave.find\_by\_employee\_id(employee\_id, :joins =\> :employee, :conditions =\> ["employees.manager\_id=?", manager\_id])

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 29, 2010, 2:11pm UTC](https://discuss.rubyonrails.org/t/sql-query-confusion/48953/3 "2010-01-29T14:11:42Z")

</div>

Hi Sharagoz,

This query is not working.. My model names are Leaves and Employee and table names are leaves and employees. So i tried this query as:- Leaves.find\_by\_employee\_id (:employee\_id, :joins =\> :employee, :conditions =\> ["employees.manager\_id = ?", session[:employee].manager\_id ] )

But no results 😢

Sharagoz -- wrote:

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 29, 2010, 2:13pm UTC](https://discuss.rubyonrails.org/t/sql-query-confusion/48953/4 "2010-01-29T14:13:22Z")

</div>

Hemant Bhargava wrote:

> Hi Sharagoz,
> 
> This query is not working.. My model names are Leaves and Employee and table names are leaves and employees. So i tried this query as:- Leaves.find\_by\_employee\_id (:employee\_id, :joins =\> :employee, :conditions =\> ["employees.manager\_id = ?", session[:employee].manager\_id ] )
> 
> But no results 😢

You shouldn't be using both find\_by\_\* and :conditions. Pick one or the other.

> Sharagoz -- wrote:
> 
> > Something like Leave.find\_by\_employee\_id(employee\_id, :joins =\> :employee, :conditions =\> ["employees.manager\_id=?", manager\_id])

Best,

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [January 29, 2010, 2:14pm UTC](https://discuss.rubyonrails.org/t/sql-query-confusion/48953/5 "2010-01-29T14:14:09Z")

</div>

> Hi Sharagoz,
> 
> This query is not working.. My model names are Leaves and Employee and table names are leaves and employees. So i tried this query as:- Leaves.find\_by\_employee\_id (:employee\_id,

You're passing a symbol which I assume you didn't want to do (you may also be more interested in find\_all\_by\_employee\_id)

Fred :joins =\> :employee,

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 29, 2010, 2:16pm UTC](https://discuss.rubyonrails.org/t/sql-query-confusion/48953/6 "2010-01-29T14:16:34Z")

</div>

Marnen Laibow-Koser wrote:

> Hemant Bhargava wrote:
> 
> > Hi Sharagoz,
> > 
> > This query is not working.. My model names are Leaves and Employee and table names are leaves and employees. So i tried this query as:- Leaves.find\_by\_employee\_id (:employee\_id, :joins =\> :employee, :conditions =\> ["employees.manager\_id = ?", session[:employee].manager\_id ] )
> > 
> > But no results 😢
> 
> You shouldn't be using both find\_by\_\* and :conditions. Pick one or the other.

Also note that the first argument in your code is different from what you were given.

Hemant, I've said this to you before and I'll say it again: go read some basic Rails tutorials. You're posting a lot of beginner questions to the list, and that's a waste of your time and ours. Please spend less time posting and more time reading.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 29, 2010, 2:18pm UTC](https://discuss.rubyonrails.org/t/sql-query-confusion/48953/7 "2010-01-29T14:18:03Z")

</div>

Marnen Laibow-Koser wrote:

> Hemant Bhargava wrote:
> 
> > Hi Sharagoz,
> > 
> > This query is not working.. My model names are Leaves and Employee and table names are leaves and employees. So i tried this query as:- Leaves.find\_by\_employee\_id (:employee\_id, :joins =\> :employee, :conditions =\> ["employees.manager\_id = ?", session[:employee].manager\_id ] )
> > 
> > But no results 😢
> 
> You shouldn't be using both find\_by\_\* and :conditions. Pick one or the other.

Ok fine.. Then i tried with using two queries:- 1) Leaves.find(:employee\_id, :joins =\> :employee, :conditions =\> ["employees.manager\_id = ?", session[:employee].manager\_id ] )

2) Leaves.find\_by\_employee\_id (:employee\_id, :joins =\> :employee)

Both of them are not working..

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 29, 2010, 2:19pm UTC](https://discuss.rubyonrails.org/t/sql-query-confusion/48953/8 "2010-01-29T14:19:57Z")

</div>

Marnen Laibow-Koser wrote:

> Marnen Laibow-Koser wrote:
> 
> > Hemant Bhargava wrote:
> > 
> > > Hi Sharagoz,
> > > 
> > > This query is not working.. My model names are Leaves and Employee and table names are leaves and employees. So i tried this query as:- Leaves.find\_by\_employee\_id (:employee\_id, :joins =\> :employee, :conditions =\> ["employees.manager\_id = ?", session[:employee].manager\_id ] )
> > > 
> > > But no results 😢
> > 
> > You shouldn't be using both find\_by\_\* and :conditions. Pick one or the other.
> 
> Also note that the first argument in your code is different from what you were given.
> 
> Hemant, I've said this to you before and I'll say it again: go read some basic Rails tutorials. You're posting a lot of beginner questions to the list, and that's a waste of your time and ours. Please spend less time posting and more time reading.

Ohh .. Ok Sorry but i have already tried it using myself.. Used googling.. Read joins .. But i came up with nothing thats why i posted my ques here.. Anewayz, I'll take care of it in future as well..

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 29, 2010, 2:34pm UTC](https://discuss.rubyonrails.org/t/sql-query-confusion/48953/9 "2010-01-29T14:34:23Z")

</div>

Hemant Bhargava wrote:

> Marnen Laibow-Koser wrote:
> 
> > Hemant Bhargava wrote:
> > 
> > > Hi Sharagoz,
> > > 
> > > This query is not working.. My model names are Leaves and Employee and table names are leaves and employees. So i tried this query as:- Leaves.find\_by\_employee\_id (:employee\_id, :joins =\> :employee, :conditions =\> ["employees.manager\_id = ?", session[:employee].manager\_id ] )
> > > 
> > > But no results 😢
> > 
> > You shouldn't be using both find\_by\_\* and :conditions. Pick one or the other.
> 
> Ok fine.. Then i tried with using two queries:- 1) Leaves.find(:employee\_id, :joins =\> :employee, :conditions =\> ["employees.manager\_id = ?", session[:employee].manager\_id ] )
> 
> 2) Leaves.find\_by\_employee\_id (:employee\_id, :joins =\> :employee)
> 
> Both of them are not working..

Because your first argument in each case is \*still\* wrong. Look carefully at the example that was given to you.

If you can't see the difference here, then you have no business programming in Ruby without further study.

---

<div class="post-metadata">

**Author:** ![Niels\_Meersschaert](https://avatars.discourse-cdn.com/v4/letter/n/ecae2f/32.png) [@Niels\_Meersschaert](https://discuss.rubyonrails.org/u/Niels_Meersschaert)\
**Post date:** [January 29, 2010, 5:21pm UTC](https://discuss.rubyonrails.org/t/sql-query-confusion/48953/10 "2010-01-29T17:21:52Z")

</div>

Part of the issue may also be that your model/table names are NOT following convention. The model should be singular, the table name plural underscored lower case:

Model Table

**Leave** leaves

Employee employees

Assume you also have these in your classes:

class Leave \< ActiveRecord::Base

belongs\_to :employee

end

class Employee \< ActiveRecord::Base

has\_many :leaves

belongs\_to :manager, :class\_name =\> “Employee”

has\_many :subordinates, :class\_name =\> “Employee”, :foreign\_key =\> “manager\_id”

end

so for Employee (Manager) with id 24:

manager = Employee.find(24)

leaves = manager.subordinates.collect(&:leaves) (You can also declare a relationship in the Employee model to mimic this if desired in a has\_many :through)

If you made manager a separate model/table you’d adjust the relationships as appropriate.

Niels
