Hi
I want to express that :- many users can work for many projects with
many roles.
I started of with a join table that has a roll attribute
Table projects_users
*project_id
*user_id
*role_name
I got most things to work but I couldnt delete 1 association properly.
Eg
def destroy_project_user_role
#render_text @params.inspect
@project = Project.find(@params[''id''])
@user= User.find(@params[''user_id''])
@project.users.delete(@user)
redirect_to :action => ''edit'', :id =>
@params[''id'']
end
deletes *all* of the user''s roles in a project which is not what I
want.
I cant see how to introduce the role_name restriction above so I decided
to throw caution to the breeze and change the DB model to a join table
with 3 id''s to see if this would make life easier :-
Table projects_users
*project_id
*user_id
*role_id
Unfortuntately I still cant see how to do it.I have a feeling that I
have create a new model, something like
class ProjectsUsersRole < ActiveRecord::Base
belongs_to :role
belongs_to :project
belongs_to :user
end
and
@pur = ProjectsUsersRole.find_all(something using
project_id,user_id,role_id)
but it looks like I am heading towards a straight SQL delete which is
not very Rails like. Is there an elegant way?
Thanks
Peter
PS my posts from work(Outlook) seem to have duplicated LFs. It will be
interesting to see if this one from home(also Outlook) does to
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Peter Cutting wrote:> I want to express that :- many users can work for many projects with > many roles. Unfortuntately I still cant see how to do it.I have a feeling > that I have create a new model, something like > > class ProjectsUsersRole < ActiveRecord::Base > belongs_to :role > belongs_to :project > belongs_to :user > end > > but it looks like I am heading towards a straight SQL delete which is > not very Rails like. Is there an elegant way?Your ProjectsUsersRole is a good idea. The join table often deserves a model of its own. Consider using two has_many associations rather than a single habtm to model the relation between Users and Projects. class User < ActiveRecord::Base has_many :assignments end class Project < ActiveRecord::Base has_many :assignments end class Assignment < ActiveRecord::Base belongs_to :project belongs_to :user belongs_to :role end Working with an Assignment (or Participant, etc) is conceptually useful. Many-to-many relations make it easy to "lose" this part of your model. jeremy
Hi I still have not managed to delete an association from my 3 FK join table. I think that I have to delete it explicitly. Something like:- Assignment.find_by_project_id_and_user_id_and_role_id(@params["id"], @params["user_id"], @params["role_id"]).destroy Unfortunately it doesn''t work:- ActiveRecord::StatementInvalid in Projects#destroy_assignment #42S22Unknown column ''id'' in ''where clause'': DELETE FROM assignments WHERE id = NULL /app/controllers/projects_controller.rb:94:in `destroy_assignment'' script/server:49 Any ideas? Peter
I have finally got it to work using:-
Assignment.delete_all(["project_id = ? AND user_id = ? AND role_id =
?",
@params[''id''], @params[''user_id''],
@params[''role_id'']])
The above is explicitly deleting an association record(and probably any
duplicates). I suspect there is a alternative way using a habtm
association-id. For example I have a:-
Class Project
has_and_belongs_to_many :users, :class_name => "User",
:join_table => "assignments"
And one attempt might be to loop thru project.users and delete
associations with the correct roll-id. But I have no idea how to mix
roll_id into the code.
On another note, I am a little bit confused by the
http://rails.rubyonrails.com/ documentation. It does not seem to
explicitly say what is a class method and what is an instance method.
Project.find(@params[''id'']).destroy
Yes, I know you can see a lot in the examples (eg Project.find implies
class method) but what about destroy ? The above line is fairly typical
and I would say that destroy is an instance method based on that. But I
cannot seem to be able to stick destroy on the end of
Assignment.find_by_project_id_and_user_id_and_role_id(@params[''id''],
@params[''user_id''],
@params[''role_id'']).destroy
Or
@assignment = Assignment.find_by_project_id_and_user_id_and_role_id(
@params[''id''], @params[''user_id''],
@params[''role_id''])
@assignment.each { |ass| ass.destroy }
I guess I am missing something fairly basic
Peter