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)
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