Derek Haynes
2005-May-03 22:40 UTC
Problem adding a record with a has_and_belongs_to_many relationship
I have a "Project" object and a "User" object. Projects can have multiple users and users can belong to multiple projects. Thus, I''ve created the has_and_belongs_to_many relationship in each class and a projects_user table(project_id,user_id). However, when I try to add a new Project record, I get the following error: undefined method `user_id='' for #<Project:0xc8269e8> This is referring to the selectbox where the user is selected: <%= select ''project'',''user_id'', User.find_all.collect {|u| [u.name,u.id] } %> I use the same syntax in a belongs_to relationship and had no problems. Any ideas on what I''m missing? Thanks in advance! - Derek
Adam M.
2005-May-03 22:51 UTC
Re: Problem adding a record with a has_and_belongs_to_many relationship
Derek Haynes wrote:><%= select ''project'',''user_id'', User.find_all.collect {|u| [u.name,u.id] } %> > >I use the same syntax in a belongs_to relationship and had no problems. > >Any ideas on what I''m missing? Thanks in advance! > >I think the problem is you are trying to use a model from a view. A view doesn''t do anything. It is just use to display stuff - a passive function. It is the controller that calls the model in response to user actions. - Adam
Mark Paxton
2005-May-03 22:51 UTC
Re: Problem adding a record with a has_and_belongs_to_many relationship
Derek Haynes wrote:> I have a "Project" object and a "User" object. > > Projects can have multiple users and users can belong to multiple projects. > > Thus, I''ve created the has_and_belongs_to_many relationship in each > class and a projects_user table(project_id,user_id). > > However, when I try to add a new Project record, I get the following error: > > undefined method `user_id='' for #<Project:0xc8269e8> > > This is referring to the selectbox where the user is selected: > > <%= select ''project'',''user_id'', User.find_all.collect {|u| [u.name,u.id] } %> > > I use the same syntax in a belongs_to relationship and had no problems. > > Any ideas on what I''m missing? Thanks in advance! >Is there a particular reason not to use: <%= collection_select("project", "user_id", @users, "id", "name") %> and def new @project = Project.new @users = User.find_all end in your project controller? Sorry if I''m misunderstanding! Mark
Pat Maddox
2005-May-03 22:52 UTC
Re: Problem adding a record with a has_and_belongs_to_many relationship
Don''t know if you made a typo or not, but the table should be projects_users, not projects_user On 5/3/05, Derek Haynes <derek.haynes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a "Project" object and a "User" object. > > Projects can have multiple users and users can belong to multiple projects. > > Thus, I''ve created the has_and_belongs_to_many relationship in each > class and a projects_user table(project_id,user_id). > > However, when I try to add a new Project record, I get the following error: > > undefined method `user_id='' for #<Project:0xc8269e8> > > This is referring to the selectbox where the user is selected: > > <%= select ''project'',''user_id'', User.find_all.collect {|u| [u.name,u.id] } %> > > I use the same syntax in a belongs_to relationship and had no problems. > > Any ideas on what I''m missing? Thanks in advance! > > - Derek > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >