Hello-
I''m having some trouble with list ordering, and I think it''s
because
I''m using the act_as_list functions incorrectly. I am hoping someone
can kick me in the right direction
Here are my model classes:
class Project < ActiveRecord::Base
	has_many :tasks,:order => "position"
end
class Task < ActiveRecord::Base
	belongs_to :project
	acts_as_list :scope => :project
end
----------
and here is an action that is *supposed to* take create a new Task
from form data, add it to the project, and move it to the bottom of
the list(and, I had assumed, shift other position values accordingly):
def add_task
	@project=Project.find(@params[''project''][''id''])
	task=Task.new
	task.attributes = @params["new_item"]
	@project.tasks << task
	task.move_to_bottom
	task.save
	@project.save
	redirect_to :action=>"show",
:id=>@params[''project''][''id'']
end
------------         
When I add four items, in this order ("one",
"two","three","four"), I
get this in my database:
nextaction_dev=# select * from tasks;
 id | description | project_id | position | completed
----+-------------+------------+----------+-----------
 38 | one         |          1 |        1 |
 39 | two         |          1 |        1 |
 40 | three       |          1 |        1 |
 41 | four        |          1 |        2 |
(4 rows)
So, A) Does anyone haev an idea of what I''m doing wrong?
		b) Is ActiveRecord supposed to be maintaining uniqueness of the
foreign key, position pair?
As I kind of expected, the right way was quite a bit simpler than the wrong way: def add_task @project=Project.find(@params[''project''][''id'']) task=Task.new task=@project.tasks.create @params["new_item"] @project.save redirect_to :action=>"show", :id=>@params[''project''][''id''] end It then seems that the list positioning logic is not in play when you append an item to a collection via "collection << item" (see my original post). Is that a bug, or expected behavior? -Ross On Mon, 21 Feb 2005 10:51:33 -0500, Ross M Karchner <rosskarchner-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello- > > I''m having some trouble with list ordering, and I think it''s because > I''m using the act_as_list functions incorrectly. I am hoping someone > can kick me in the right direction > > Here are my model classes: > > class Project < ActiveRecord::Base > has_many :tasks,:order => "position" > end > > class Task < ActiveRecord::Base > belongs_to :project > acts_as_list :scope => :project > end > > ---------- > and here is an action that is *supposed to* take create a new Task > from form data, add it to the project, and move it to the bottom of > the list(and, I had assumed, shift other position values accordingly): > > def add_task > @project=Project.find(@params[''project''][''id'']) > task=Task.new > task.attributes = @params["new_item"] > @project.tasks << task > task.move_to_bottom > task.save > @project.save > redirect_to :action=>"show", :id=>@params[''project''][''id''] > > end > ------------ > > When I add four items, in this order ("one", "two","three","four"), I > get this in my database: > > nextaction_dev=# select * from tasks; > id | description | project_id | position | completed > ----+-------------+------------+----------+----------- > 38 | one | 1 | 1 | > 39 | two | 1 | 1 | > 40 | three | 1 | 1 | > 41 | four | 1 | 2 | > (4 rows) > > So, A) Does anyone haev an idea of what I''m doing wrong? > b) Is ActiveRecord supposed to be maintaining uniqueness of the > foreign key, position pair? >
Here''s some more fun (sick of me, yet?)...
Given this table, created with the code sent previously:
nextaction_dev=# select * from tasks;
 id | description | project_id | position | 
----+-------------+------------+----------+-----------
 80 | One         |          1 |        1 |
 81 | Two         |          1 |        2 |
 82 | Three       |          1 |        3 |
 83 | Four        |          1 |        4 |
 84 | Five         |          1 |        5 |
(5 rows)
Looks Beautiful!
If I call .destroy on Task id 82 ("Three"), things seem to go wrong:
nextaction_dev=# select * from tasks;
 id | description | project_id | position | completed
----+-------------+------------+----------+-----------
 80 | One         |          1 |        1 |
 81 | Two         |          1 |        2 |
 83 | Four        |          1 |        3 |
 84 | Five        |          1 |        3 |
(4 rows)
It makes sense that 83 would fold up to the third position, but why is
84 doing that also?
Also, bot 83 and 84 return true when ".last?" is called.
So this is the same as my last question. I''m not comfortable enough
yet with rails to say for certain whether these are bugs I should
report, or if I am doing something wrong.
Thanks in advance for any guidance!
-Ross
On Wed, 23 Feb 2005 10:09:23 -0500, Ross M Karchner
<rosskarchner-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> As I kind of expected, the right way was quite a bit simpler than the wrong
way:
> 
> def add_task
>        
@project=Project.find(@params[''project''][''id''])
>         task=Task.new
>         task=@project.tasks.create @params["new_item"]
>         @project.save
>         redirect_to :action=>"show",
:id=>@params[''project''][''id'']
> end
> 
> It then seems that the list positioning logic is not in play when you
> append an item to a collection via "collection << item"
(see my
> original post).
> 
> Is that a bug, or expected behavior?
> 
> -Ross
> 
> 
> On Mon, 21 Feb 2005 10:51:33 -0500, Ross M Karchner
> <rosskarchner-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > Hello-
> >
> > I''m having some trouble with list ordering, and I think
it''s because
> > I''m using the act_as_list functions incorrectly. I am hoping
someone
> > can kick me in the right direction
> >
> > Here are my model classes:
> >
> > class Project < ActiveRecord::Base
> >         has_many :tasks,:order => "position"
> > end
> >
> > class Task < ActiveRecord::Base
> >         belongs_to :project
> >         acts_as_list :scope => :project
> > end
> >
> > ----------
> > and here is an action that is *supposed to* take create a new Task
> > from form data, add it to the project, and move it to the bottom of
> > the list(and, I had assumed, shift other position values accordingly):
> >
> > def add_task
> >        
@project=Project.find(@params[''project''][''id''])
> >         task=Task.new
> >         task.attributes = @params["new_item"]
> >         @project.tasks << task
> >         task.move_to_bottom
> >         task.save
> >         @project.save
> >         redirect_to :action=>"show",
:id=>@params[''project''][''id'']
> >
> > end
> > ------------
> >
> > When I add four items, in this order ("one",
"two","three","four"), I
> > get this in my database:
> >
> > nextaction_dev=# select * from tasks;
> >  id | description | project_id | position | completed
> > ----+-------------+------------+----------+-----------
> >  38 | one         |          1 |        1 |
> >  39 | two         |          1 |        1 |
> >  40 | three       |          1 |        1 |
> >  41 | four        |          1 |        2 |
> > (4 rows)
> >
> > So, A) Does anyone haev an idea of what I''m doing wrong?
> >                 b) Is ActiveRecord supposed to be maintaining
uniqueness of the
> > foreign key, position pair?
> >
>
I''ve had exactly this problem before. If I remember correctly, it turned out that I was calling destroy on the record twice. On 24/02/2005, at 5:10 AM, Ross M Karchner wrote:> Here''s some more fun (sick of me, yet?)... > > Given this table, created with the code sent previously: > > nextaction_dev=# select * from tasks; > id | description | project_id | position | > ----+-------------+------------+----------+----------- > 80 | One | 1 | 1 | > 81 | Two | 1 | 2 | > 82 | Three | 1 | 3 | > 83 | Four | 1 | 4 | > 84 | Five | 1 | 5 | > (5 rows) > > Looks Beautiful! > > If I call .destroy on Task id 82 ("Three"), things seem to go wrong: > > nextaction_dev=# select * from tasks; > id | description | project_id | position | completed > ----+-------------+------------+----------+----------- > 80 | One | 1 | 1 | > 81 | Two | 1 | 2 | > 83 | Four | 1 | 3 | > 84 | Five | 1 | 3 | > (4 rows) > > It makes sense that 83 would fold up to the third position, but why is > 84 doing that also? > > Also, bot 83 and 84 return true when ".last?" is called. > > So this is the same as my last question. I''m not comfortable enough > yet with rails to say for certain whether these are bugs I should > report, or if I am doing something wrong. > > Thanks in advance for any guidance! > > -Ross > > On Wed, 23 Feb 2005 10:09:23 -0500, Ross M Karchner > <rosskarchner-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> As I kind of expected, the right way was quite a bit simpler than the >> wrong way: >> >> def add_task >> @project=Project.find(@params[''project''][''id'']) >> task=Task.new >> task=@project.tasks.create @params["new_item"] >> @project.save >> redirect_to :action=>"show", :id=>@params[''project''][''id''] >> end >> >> It then seems that the list positioning logic is not in play when you >> append an item to a collection via "collection << item" (see my >> original post). >> >> Is that a bug, or expected behavior? >> >> -Ross >> >> >> On Mon, 21 Feb 2005 10:51:33 -0500, Ross M Karchner >> <rosskarchner-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> Hello- >>> >>> I''m having some trouble with list ordering, and I think it''s because >>> I''m using the act_as_list functions incorrectly. I am hoping someone >>> can kick me in the right direction >>> >>> Here are my model classes: >>> >>> class Project < ActiveRecord::Base >>> has_many :tasks,:order => "position" >>> end >>> >>> class Task < ActiveRecord::Base >>> belongs_to :project >>> acts_as_list :scope => :project >>> end >>> >>> ---------- >>> and here is an action that is *supposed to* take create a new Task >>> from form data, add it to the project, and move it to the bottom of >>> the list(and, I had assumed, shift other position values >>> accordingly): >>> >>> def add_task >>> @project=Project.find(@params[''project''][''id'']) >>> task=Task.new >>> task.attributes = @params["new_item"] >>> @project.tasks << task >>> task.move_to_bottom >>> task.save >>> @project.save >>> redirect_to :action=>"show", :id=>@params[''project''][''id''] >>> >>> end >>> ------------ >>> >>> When I add four items, in this order ("one", "two","three","four"), I >>> get this in my database: >>> >>> nextaction_dev=# select * from tasks; >>> id | description | project_id | position | completed >>> ----+-------------+------------+----------+----------- >>> 38 | one | 1 | 1 | >>> 39 | two | 1 | 1 | >>> 40 | three | 1 | 1 | >>> 41 | four | 1 | 2 | >>> (4 rows) >>> >>> So, A) Does anyone haev an idea of what I''m doing wrong? >>> b) Is ActiveRecord supposed to be maintaining >>> uniqueness of the >>> foreign key, position pair? >>> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >