Pierre Pierrot
2008-Apr-23 10:37 UTC
self referential (n-n relationship) belongs many controller
Hi all, I ve some troubles in my small acpplication (only 2 tables). I want to follow up tasks but these tasks are linked to others tasks. So I ve tried to do store the nn relationship in other table called links 1st table : tasks columns : id | infos neutral... (as title, description etc ...) ----------------------------------------------------------------------------------- model : class Task < ActiveRecord::Base belongs_to :statut has_many :links has_many :tasklinks , :through => :links end --------------------------------------------------------------------------------------- 2e table : links columns : id | task_id | tasklink_id so in this table, the tasklink_id are the key for the associated tasks to task_id --------------------------------------------------------------------------------------- model : class Link < ActiveRecord::Base belongs_to :task belongs_to :tasklink, :class_name => ''Task'', :foreign_key => ''tasklink_id'' end ---------------------------------------------------------------------------------------- I have to link the tasks in a kind of auto join in nn relationship (1-n can be easily solved with act_as_tree) so ... <b>THE PROBLEM :</b> I ve my problem in the create and update of task controller : rows in table links are not created (or badly : task_id is ok, but tasklink_id is KO) Here is my tasks_controller : ------------------------------------------------------------------------------------- class TasksController < ApplicationController helper :sorting def index list @statuts=Statut.find_all render :action => ''list'' end # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) verify :method => :post, :only => [ :destroy, :create, :update ], :redirect_to => { :action => :list } def list @task_pages, @tasks = paginate :tasks, :per_page => 50 @statuts=Statut.find_all @sorter = SortingHelper::Sorter.new(self, params[''sort''], params[''order''], ''id'', ''ASC'') search = {:id => @params[:id], :statut_id => @params[:statut_id], :description => @params[:description], :acteur => @params[:acteur]} if search[:id] && search[:id] != '''' query ||= [] query_params ||= [] query << "id = ?" query_params += [search[:id]] end if search[:statut_id] && search[:statut_id] != '''' query ||= [] query_params ||= [] query << "statut_id = ?" query_params += [search[:statut_id]] end if search[:description] && search[:description] != '''' query ||= [] query_params ||= [] query << "description LIKE ?" query_params += ["%" + search[:description] + "%" ] end if search[:acteur] && search[:acteur] != '''' query ||= [] query_params ||= [] query << "acteur LIKE ?" query_params += ["%" + search[:acteur] + "%" ] end @conditions = [ query * '' AND ''] + query_params if query @task_pages, @tasks = paginate :tasks, :per_page => 50, :order => @sorter.to_sql, :conditions => @conditions end def show @task = Task.find(params[:id]) @statuts = Statut.find_all #c est la loose ici end def new @task = Task.new #sapin @link = Link.new #sapin @links = Link.find_all @statuts = Statut.find_all @tasks = Task.find_all @sorter = SortingHelper::Sorter.new(self, params[''sort''], params[''order''], ''tasklink_id'', ''ASC'') end def create @task = Task.new(params[:task]) @link = Link.new(params[:link]) @links = Link.find_all @tasks = Task.find_all @statuts = Statut.find_all #if @params[:task_ids] #@task.tasklinks = Task.find(params[:task_ids]) #else #@substance.risques = [] #end #ca foire un max if @task.save @link.task_id = @task.id @link.tasklink_id = Task.find(??????).task_id @link.save flash[:notice] = ''L action a été créée'' redirect_to :action => ''list'' else flash[:warn]="l action n a pas été créee" render :action => ''new'' end end def edit @task = Task.find(params[:id]) @statuts = Statut.find_all @tasks = Task.find_all @links = Link.find_all @sorter = SortingHelper::Sorter.new(self, params[''sort''], params[''order''], ''task_id'', ''ASC'') @task_pages, @tasks = paginate :tasks, :per_page => 50, :order => @sorter.to_sql, :conditions => @conditions end def update @task = Task.find(params[:id]) @statuts = Statut.find_all if @task.update_attributes(params[:task]) flash[:notice] = ''Task was successfully updated.'' redirect_to :action => ''show'', :id => @task else render :action => ''edit'' end end def destroy Task.find(params[:id]).destroy redirect_to :action => ''list'' end end -------------------------------------------------------------------------------------- -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---