jerome.lacoste-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Oct-30 18:31 UTC
has_many throught and manual handling of updates. Is there something easier ?
Given the model
class Project < ActiveRecord::Base
has_many :assignments
has_many :assignees, :class_name => "User", :through =>
:assignments,
:source => :user
class User < ActiveRecord::Base
has_many :assignments;
class Assignment < ActiveRecord::Base
belongs_to :project
belongs_to :user
I am currently updating the project assignees in my
ProjectsController.update
I first tried to do something like
@project.assignees = User.find(params[:assignee_ids])
but this fails as rails cannot know how to initialize my assignments. I
wish it could!
I''ve read
http://blog.hasmanythrough.com/articles/2006/08/19/magic-join-model-creation
and
http://blog.hasmanythrough.com/articles/2006/04/17/join-models-not-proxy-collections.
So I am updating my table doing something like:
def update
@project = Project.find(params[:id])
@users = User.find(:all)
if request.get?
render :action => ''edit''
else
if params[:assignee_ids]
# FIXME can''t this be railsified somehow ?? this is ugly...
new_assignees = User.find(params[:assignee_ids])
# remove old ones...
@project.assignees.each do |old_assignee|
if ! new_assignees.include? old_assignee
Assignment.delete_all("user_id = #{old_assignee.id} and
project_id = #{@project.id}")
end
end
# add the new ones
new_assignees.each do |new_assignee|
# remove those who are not present anymore
if ! @project.assignments.include? new_assignee
@project.assignments.create!(:user => new_assignee)
end
end
else
# this won''t work because I don''t have an id column...
# @project.assignments.clear
Assignment.delete_all("project_id = #{@project.id}")
end
@project.assignments(true)
if @project.update_attributes(params[:project])
flash[:notice] = ''Project was successfully updated.''
redirect_to :action => ''edit'', :id => @project
else
render :action => ''edit''
end
end
end
Is there something simpler/cleaner ?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
jerome.lacoste-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Oct-30 19:07 UTC
has_many throught and manual handling of updates. Is there something easier ?
Given the model
class Project < ActiveRecord::Base
has_many :assignments
has_many :assignees, :class_name => "User", :through =>
:assignments,
:source => :user
class User < ActiveRecord::Base
has_many :assignments;
class Assignment < ActiveRecord::Base
belongs_to :project
belongs_to :user
I am currently updating the project assignees in my
ProjectsController.update
I first tried to do something like
@project.assignees = User.find(params[:assignee_ids])
but this fails as rails cannot know how to initialize my assignments. I
wish it could!
I''ve read
http://blog.hasmanythrough.com/articles/2006/08/19/magic-join-model-creation
and
http://blog.hasmanythrough.com/articles/2006/04/17/join-models-not-proxy-collections.
So I am updating my table doing something like:
def update
@project = Project.find(params[:id])
@users = User.find(:all)
if request.get?
render :action => ''edit''
else
if params[:assignee_ids]
# FIXME can''t this be railsified somehow ?? this is ugly...
new_assignees = User.find(params[:assignee_ids])
# remove old ones...
@project.assignees.each do |old_assignee|
if ! new_assignees.include? old_assignee
Assignment.delete_all("user_id = #{old_assignee.id} and
project_id = #{@project.id}")
end
end
# add the new ones
new_assignees.each do |new_assignee|
# remove those who are not present anymore
if ! @project.assignments.include? new_assignee
@project.assignments.create!(:user => new_assignee)
end
end
else
# this won''t work because I don''t have an id column...
# @project.assignments.clear
Assignment.delete_all("project_id = #{@project.id}")
end
@project.assignments(true)
if @project.update_attributes(params[:project])
flash[:notice] = ''Project was successfully updated.''
redirect_to :action => ''edit'', :id => @project
else
render :action => ''edit''
end
end
end
Is there something simpler/cleaner ?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---