Hi Adam,
For the relationships that you have suggested, I think the associations are
Project.rb
has_and_belongs_to_many :Users
has_and_belongs_to_many :Companies
User.rb
has_and_belongs_to_many :Projects
belongs_to :Company
Company.rb
has_and_belongs_to_many :Projects
has_many :Users
Then I think the syntax to get a list of the companies projects are
@company.projects
provided you have a valid company in the @company variable.
Likewise to find the companies working on a given project
@project.companies
I believe this can return nil.
Hope this helps
Dan
On 10/10/05, Adam Groves
<adam.groves-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> I''m writing an application which has a Project, Company and User
class and
> associated tables.
>
> A Project can have many Companies and like wise a Company can have many
> Projects. Users belong to one Company and each Company has many Users. But
> not all Users in a Company are necessarily involved in all of the Companies
> Projects.
>
> I''m thinking that these Associations best describe the situation:
>
> Project.rb has_and_belongs_to_many :Users
> User.rb has_and_belongs_to_many :Projects
> belongs_to :Company
> Company.rb has_many :Users
>
> I then have 3 list.rhtml files for each Controller so that:
>
> projects/list.rhtml lists the projects as links to;
> companies/list.rhtml which lists the companies for that project as links
> to;
> users/list.rhtml which lists the users for that company working on that
> project
>
> I''m really not sure of the best way to list the Companies working
on that
> project. I was thinking of using find_by_sql on users finding those that
> work on the specific Project and returing the distinct company.ids
>
> Is there a better rails way using .find? Are my model associations wrong?
>
> Would appreciate any help or advice.
>
> Kind regards
>
> Adam
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails