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..
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 ..
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 ] )
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])
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)
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.
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 ] )
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..
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 ] )
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:
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.