i have a Project model, which belongs_to :user when i add a project, the users_id is based on the session[:users_id]. to get the users_id into the projects table, i''m doing this: @project = Project.new(params[:project]) @project.users_id = session[:users_id] this is working, i''m just verifying this is the best way to do it.
On Feb 18, 2006, at 9:53 AM, matthew collins wrote:> i have a Project model, which belongs_to :user > > when i add a project, the users_id is based on the session[:users_id]. > > to get the users_id into the projects table, i''m doing this: > > @project = Project.new(params[:project]) > @project.users_id = session[:users_id] > > this is working, i''m just verifying this is the best way to do it. >Yup, that looks good, Matthew. You may want to change "users_id" to "user_id", however, since the latter is the Rails naming convention and will make your belongs_to and has_many associations work without specifiying a foreign_key. Duane Johnson (canadaduane) http://blog.inquirylabs.com/
On Feb 18, 2006, at 8:53 AM, matthew collins wrote:> to get the users_id into the projects table, i''m doing this: > > @project = Project.new(params[:project]) > @project.users_id = session[:users_id] > > this is working, i''m just verifying this is the best way to do it.It''s more Rails idiomatic to let the associations handle the assignment, and to use singular foreign key naming: If you have a @user object around: @project = Project.new(params[:project]) @project.user = @user -- -- Tom Mornini
Even better (again assuming the presence of a loaded user object): @project = @user.project.build( params[:project] ) @project.save On 2/18/06, Tom Mornini <tmornini@infomania.com> wrote:> > On Feb 18, 2006, at 8:53 AM, matthew collins wrote: > > > to get the users_id into the projects table, i''m doing this: > > > > @project = Project.new(params[:project]) > > @project.users_id = session[:users_id] > > > > this is working, i''m just verifying this is the best way to do it. > > It''s more Rails idiomatic to let the associations handle the assignment, > and to use singular foreign key naming: > > If you have a @user object around: > > @project = Project.new(params[:project]) > @project.user = @user > > -- > -- Tom Mornini > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers, Luke Redpath www.lukreedpath.co.uk -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060219/d5c89ab2/attachment.html
Sorry, the above is assuming a has_one relationship between user and project. If it is has_many, you want: @project = @user.projects.build( params[:project] ) On 2/19/06, Luke Redpath <contact@lukeredpath.co.uk> wrote:> > Even better (again assuming the presence of a loaded user object): > > @project = @user.project.build( params[:project] ) > @project.save > > On 2/18/06, Tom Mornini <tmornini@infomania.com> wrote: > > > > On Feb 18, 2006, at 8:53 AM, matthew collins wrote: > > > > > to get the users_id into the projects table, i''m doing this: > > > > > > @project = Project.new(params[:project]) > > > @project.users_id = session[:users_id] > > > > > > this is working, i''m just verifying this is the best way to do it. > > > > It''s more Rails idiomatic to let the associations handle the assignment, > > and to use singular foreign key naming: > > > > If you have a @user object around: > > > > @project = Project.new(params[:project]) > > @project.user = @user > > > > -- > > -- Tom Mornini > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > -- > Cheers, > Luke Redpath > www.lukreedpath.co.uk-- Cheers, Luke Redpath www.lukreedpath.co.uk -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060219/c6693243/attachment.html