Whats the best way to achieve this?

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.

Project.find(id).employees

On 4/13/06, James W. [email protected] 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/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Ben R.
http://www.benr75.com

Ben R. wrote:

Project.find(id).employees

On 4/13/06, James W. [email protected] 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/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Ben R.
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.

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 :slight_smile:

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://read.saush.com
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.

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 :slight_smile:

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://read.saush.com
http://jaccal.sourceforge.net

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 :slight_smile:

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://read.saush.com
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.

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 :slight_smile:

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://read.saush.com
http://jaccal.sourceforge.net

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