Need for multiple acts_as_list

I have a model “Childmodel” that belongs_to two other models “Parent1”
and “Parent2”. “Parent1” “has_many :childmodels, :order => :positionp1”
and “Parent2” “has_many :childmodels, :order => :positionp2”. i.e. The
child is independently positioned within each of its parents.

This works fine and gets me the useful methods associated with
belongs_to and has_many. However, I would like to use acts_as_list on
the Childmodel in order to have the order fields set when I do
Childmodel.new.

At present, acts_as_list only works with a single parent/child
relationship. It finds the highest-used order field in a before_create
callback with SQL like “SELECT * FROM parent WHERE parent_id =
#{parent_id} ORDER position DESC LIMIT 1”. It adds 1 to this and stores
it in the new record.

I am thinking I might have a go at modifying acts_as_list to support
multiple occurrences, but first I’d be interested in people’s thoughts
on the best API for this. For example, the method “child.move_higher”
would have to become “child.move_higher_in_parent1” or
“child.parent1_list.move_higher”. Are there any better suggestions?

The acts_as_list method could become:

acts_as_list :parent => :parent1, :scope => :parent1_id, :column =>
:position_within_parent1

Any ideas on this gratefully received. If anyone else has a solution (or
would like to develop one) that would be great! Also, if anyone is
already doing this. Also, if anyone can see any logical gotchas in this
approach.

Julian

Am Mittwoch, den 22.03.2006, 11:25 +0100 schrieb Julian G.:

At present, acts_as_list only works with a single parent/child

The acts_as_list method could become:

acts_as_list :parent => :parent1, :scope => :parent1_id, :column =>
:position_within_parent1

Any ideas on this gratefully received. If anyone else has a solution (or
would like to develop one) that would be great! Also, if anyone is
already doing this. Also, if anyone can see any logical gotchas in this
approach.

http://wiki.rubyonrails.com/rails/pages/Acts+as+HABTM+List


Norman T.

http://blog.inlet-media.de

Norman T. wrote:

Peak Obsession

Thanks for that. A couple of questions.

  1. Does it set the ‘position’ field on record creation?
  2. Do you have apointer to any documentation on writing a plugin like
    this? It would be a good approach if I create my own?

Julian

Norman T. wrote:

Maybe you can a little bit clearer in what the functionality of
acts_as_habtm_list differs from what you want to to.

Mainly, I need acts_as_list to work with has_many and belongs_to, rather
than has_and_belongs_to_many.

And thanks very much for those plugin pointers. It doesn’t look too
difficult.

Julian

Am Mittwoch, den 22.03.2006, 12:12 +0100 schrieb Julian G.:

Norman T. wrote:

Peak Obsession

Thanks for that. A couple of questions.

  1. Does it set the ‘position’ field on record creation?
  2. Do you have apointer to any documentation on writing a plugin like
    this? It would be a good approach if I create my own?
  1. It automatically sets the position field after setting the
    association, means after adding to a parent. This can be at creation
    time.

  2. It is derived from acts_as_list. A general example how to write a
    plugin can be found here:

http://wiki.rubyonrails.org/rails/pages/HowTosPlugins

Maybe you can a little bit clearer in what the functionality of
acts_as_habtm_list differs from what you want to to. Maybe you can
modify or extend the plugin.


Norman T.

http://blog.inlet-media.de

Could you use a family model where a user can have_many families,
belong_to
family where the family has_one mother, has_one father, has_many
children?

Then the children could act_as_list within the scope of the family.

Am Mittwoch, den 22.03.2006, 13:48 +0100 schrieb Julian G.:

acts_as_habtm_list would help, although I don’t think it would allow for
each child to have a different position in each family.

That is exactly what acts_as_habtm_list does! You can have multiple
parents with individual position of the child within a parent scope.

I’ll give you an example:

class Product < ActiveRecord::Base
has_and_belongs_to :groups
acts_as_habtm_list :scope => :groups
end

class Group < ActiveRecord::Base
has_and_belongs_to :products
end

Each Product has it’s individual position within a Group scope, even the
Product is assigned to multiple Groups.


Norman T.

http://blog.inlet-media.de

Daniel ----- wrote:

Could you use a family model where a user can have_many families,
belong_to
family where the family has_one mother, has_one father, has_many
children?

Then the children could act_as_list within the scope of the family.

A very good example. But suppose parents remarry and one child is now
part of another family too. I think this is where Norman’s
acts_as_habtm_list would help, although I don’t think it would allow for
each child to have a different position in each family.

A better example for my requirement would be a child belonging to a
family and to a school. The family and the school list both have a
position for the child.

Julian

Norman T. wrote:

That is exactly what acts_as_habtm_list does! You can have multiple
parents with individual position of the child within a parent scope.

I’ll give you an example:

class Product < ActiveRecord::Base
has_and_belongs_to :groups
acts_as_habtm_list :scope => :groups
end

class Group < ActiveRecord::Base
has_and_belongs_to :products
end

Each Product has it’s individual position within a Group scope, even the
Product is assigned to multiple Groups.

OK. So you presumably store the position in the intermediate join table?

Julian

Am Mittwoch, den 22.03.2006, 16:12 +0100 schrieb Julian G.:

class Group < ActiveRecord::Base
has_and_belongs_to :products
end

Each Product has it’s individual position within a Group scope, even the
Product is assigned to multiple Groups.

OK. So you presumably store the position in the intermediate join table?

Yes. Just add the position column to your join table, the rest is done
automagically.


Norman T.

http://blog.inlet-media.de

I have a question about how best to setup something similiar to this.
Think
of a Netflix queue, where each user has their own queue with their own
rankings of movies. In the work I am doing, everyone has the same
“movies”
in their “queue”, but each is allowed to re-order their queue separately
from each other.

The final output of this is being able to see the aggregate
stack-ranking of
items in the overall queues to judge popularity of the items.

I have models for users & items, and a join table for the rankings. I
couldn’t figure out how to set the scope to multiple tables in
acts_as_list,
but thanks to this thread I see there is an option I could add for
acts_as_habtm_list.

Is this what folks here would recommend?

Many thanks,

Brian