Forum: Ruby on Rails Whats the best way to achieve this?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
James W. (Guest)
on 2006-04-13 19:37
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.
Ben R. (Guest)
on 2006-04-13 19:50
(Received via mailing list)
Project.find(id).employees

On 4/13/06, James W. <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/.
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Ben R.
http://www.benr75.com
James W. (Guest)
on 2006-04-13 19:53
Ben R. wrote:
> Project.find(id).employees
>
> On 4/13/06, James W. <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/.
>> _______________________________________________
>> Rails mailing list
>> removed_email_address@domain.invalid
>> 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. (Guest)
on 2006-04-13 19:59
(Received via mailing list)
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://read.saush.com
http://jaccal.sourceforge.net
James W. (Guest)
on 2006-04-13 20:06
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://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.
Chang Sau S. (Guest)
on 2006-04-13 20:12
(Received via mailing list)
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://read.saush.com
http://jaccal.sourceforge.net
James W. (Guest)
on 2006-04-14 01:02
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://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.
James W. (Guest)
on 2006-04-14 01:34
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
This topic is locked and can not be replied to.