# Self referential (n-n relationship) belongs many controller

**URL:** <https://www.ruby-forum.com/t/self-referential-n-n-relationship-belongs-many-controller/226004>\
**Category:** Rails\
**Created:** [April 23, 2008, 12:37pm UTC](https://www.ruby-forum.com/t/self-referential-n-n-relationship-belongs-many-controller/226004 "2008-04-23T12:37:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pierre](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/d07c76/32.png) [@pierre](https://www.ruby-forum.com/u/pierre)\
**Post date:** [April 23, 2008, 12:37pm UTC](https://www.ruby-forum.com/t/self-referential-n-n-relationship-belongs-many-controller/226004/1 "2008-04-23T12:37:46Z")

</div>

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 …

**THE PROBLEM :**

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

[URIs, Addressability, and the use of HTTP GET and POST](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

* * *
