# Whats the best way to achieve this?

**URL:** <https://www.ruby-forum.com/t/whats-the-best-way-to-achieve-this/93908>\
**Category:** Rails\
**Created:** [April 13, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-achieve-this/93908 "2006-04-13T17:37:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 13, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-achieve-this/93908/1 "2006-04-13T17:37:04Z")

</div>

employees HABTM projects, projects HABTM employees.

I am doing a permissions page for a selected project. I know that I can  
get all of the employees in the system (@employee.find(:all)) or get all  
the employees for the selected project.

What i’m trying to do is get a list of all the employees in the system  
and have a check box that states if they are associated with the  
current project or not.

Any ideas please, i’m not asking for code really just a theory of how  
you would possible go about it.

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 13, 2006, 5:50pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-achieve-this/93908/2 "2006-04-13T17:50:26Z")

</div>

Project.find(id).employees

On 4/13/06, James W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Any ideas please, i’m not asking for code really just a theory of how  
> you would possible go about it.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Ben R.  
[http://www.benr75.com](http://www.benr75.com)

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 13, 2006, 5:53pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-achieve-this/93908/3 "2006-04-13T17:53:53Z")

</div>

Ben R. wrote:

> Project.find(id).employees
> 
> On 4/13/06, James W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Any ideas please, i’m not asking for code really just a theory of how  
> > you would possible go about it.
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> –  
> Ben R.  
> [http://www.benr75.com](http://www.benr75.com)

Yes that would just get the employees that were associated with the  
current project. I also need the rest of the employees that are not  
associated. Both displayed in a list ordered by name with non repeated.

The check-box would indicate if the employee was associated with the  
project or not. By checking the box that would then add the employee to  
the project.

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 13, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-achieve-this/93908/4 "2006-04-13T18:06:42Z")

</div>

Chang Sau S. wrote:

> Maybe something like this:
> 
> all\_employees = Employee.find\_all # to find all employees in the system  
> in an array  
> project\_employees = Project.find(id).employees # to find all employees  
> related to this project in an array
> 
> other\_employees = all\_employees - project\_employees # a list of  
> employees not in the project
> 
> Not sure if this is what you want though 🙂
> 
> James W. wrote:
> 
> > Any ideas please, i’m not asking for code really just a theory of how  
> > you would possible go about it.
> 
> –  
> Sau S.
> 
> [http://blog.saush.com](http://blog.saush.com)  
> [http://read.saush.com](http://read.saush.com)  
> [http://jaccal.sourceforge.net](http://jaccal.sourceforge.net)

Not quite what I was after though. I just wanted to be able to have a  
list off all the employees ordered by name ASC.

Each employee would have a checkbox after there name. That check box  
would be checked if they had an association to the current project.

If they did not then the checkbox would be unchecked.

Checking the box (or unchecking ) would the add or remove the  
association to that project.

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 13, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-achieve-this/93908/5 "2006-04-13T17:59:47Z")

</div>

Maybe something like this:

all\_employees = Employee.find\_all # to find all employees in the system  
in an array  
project\_employees = Project.find(id).employees # to find all employees  
related to this project in an array

other\_employees = all\_employees - project\_employees # a list of  
employees not in the project

Not sure if this is what you want though 🙂

James W. wrote:

> Any ideas please, i’m not asking for code really just a theory of how  
> you would possible go about it.

–  
Sau S.

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

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 13, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-achieve-this/93908/6 "2006-04-13T23:02:04Z")

</div>

Chang Sau S. wrote:

> Just at the top of my head, I think it would be simpler if you add a  
> method in your employee model that determines if the employee is related  
> to the project or not:
> 
> def associated\_with\_project?(project)  
> if self.projects.contains? project then  
> return true  
> else  
> return false  
> end  
> end
> 
> I suppose this can be done in a cleverer way somehow but this should  
> work 🙂
> 
> James W. wrote:
> 
> > > employees not in the project
> > > 
> > > >
> > 
> > Each employee would have a checkbox after there name. That check box  
> > would be checked if they had an association to the current project.
> > 
> > If they did not then the checkbox would be unchecked.
> > 
> > Checking the box (or unchecking ) would the add or remove the  
> > association to that project.
> 
> –  
> Sau S.
> 
> [http://blog.saush.com](http://blog.saush.com)  
> [http://read.saush.com](http://read.saush.com)  
> [http://jaccal.sourceforge.net](http://jaccal.sourceforge.net)

Sounds good to me, I never thought of adding something like this into  
the model. All this MVC i’m still learning.

However taking what you have here I get an error of:

undefined method `contains?’ for Project:Class

I cannot seem to find the contains? method in the API reference  
anywhere.Is it a ruby method? Googling contains? reveals nothing.

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 13, 2006, 6:12pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-achieve-this/93908/7 "2006-04-13T18:12:38Z")

</div>

Just at the top of my head, I think it would be simpler if you add a  
method in your employee model that determines if the employee is related  
to the project or not:

def associated\_with\_project?(project)  
if self.projects.contains? project then  
return true  
else  
return false  
end  
end

I suppose this can be done in a cleverer way somehow but this should  
work 🙂

James W. wrote:

> > employees not in the project
> > 
> > >
> 
> Each employee would have a checkbox after there name. That check box  
> would be checked if they had an association to the current project.
> 
> If they did not then the checkbox would be unchecked.
> 
> Checking the box (or unchecking ) would the add or remove the  
> association to that project.

–  
Sau S.

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

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 13, 2006, 11:34pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-achieve-this/93908/8 "2006-04-13T23:34:42Z")

</div>

Found that using rubys include? method seems to provisionaly work. Just  
need to add the checkbox’s in and do a full test.

def associated\_with\_project?(project)  
if self.projects.include? project then  
true  
else  
false  
end  
end
