[ANN] acts_as_habtm_list plugin


#1

I’d like to announce acts_as_habtm_list plugin for rails.

acts_as_habtm_list is a Ruby on Rails plugin, that can manage ordered
lists through a join-table. It is providing almost the same Api as
acts_as_list. The position column has to be defined in the join table.

You can find additional information at:

http://www.inlet-media.de/acts_as_habtm_list/

Install it executing this command in your rails-application root
directory:

script/plugin install
http://svn.inlet-media.de/svn/rails_extensions/plugins/acts_as_habtm_list


Norman T.

http://blog.inlet-media.de


#2

Cool!

Does this work on Rails 1.0.0? or is edge rails necessary?
-Larry

On 2/22/06, Norman T. removed_email_address@domain.invalid wrote:


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Best Regards,
-Larry
“Work, work, work…there is no satisfactory alternative.”
— E.Taft Benson


#3

Am Mittwoch, den 22.02.2006, 05:14 -0800 schrieb Larry K.:

Does this work on Rails 1.0.0? or is edge rails necessary?

No Edge Rails necessary! It works on Rails 1.0.0


Norman T.

http://blog.inlet-media.de


#4

Looks like you still have some domain-specific code in it…

def scope_condition
“#{configuration[:scope_foreign_key]} = #{category_id}”
end

Note the category_id reference.

Bob S.
http://www.railtie.net/


#5

Am Mittwoch, den 22.02.2006, 08:43 -0800 schrieb Bob S.:

Looks like you still have some domain-specific code in it…

Thank you, Bob! I fixed it.


Norman T.

http://blog.inlet-media.de


#6

Norman,
Out of couriosity, how does this differ from act_as_nested_set?
Thanks for Your Hard Work,
Rob K.


#7

Am Mittwoch, den 22.02.2006, 10:38 -0800 schrieb Rob K.:

Out of couriosity, how does this differ from act_as_nested_set?

A child can have multiple parents. All children under a specific parent
have a sort order. The same children under another parent may have a
different order.


Norman T.

http://blog.inlet-media.de


#8

I’m having real difficulty understanding how to use this plugin.

I have the following:

Artist has many artworks and categories
Categories and artworks have one artist

Categories have many artworks
Artworks belong to many categories
(a habtm relationship?)

The big deal here is that I want artworks to be ordered within their
specific categories though they may belong to many. (this is what your
plugin does) however, I can’t find any documentation that tells me how
to create that relationship to begin with. The user can make a bunch of
artworks, and make a bunch of categories. Then they can open a view that
shows the category box on the left and the list of available artworks on
the right and they can then drag and drop the artworks backward and
forward between the lists, and sort them within the categories list.

How exactly do I do this? as the moment I drop an artwork onto the
category it’s going to send the array of artworks in the category back
to the controller which then needs to run through them and update their
position in the join table somehow.

I’m a bit of a n00b and the lack of documentation has caught me off
guard here :slight_smile:

Cheers,

Brendon M.

Norman T. wrote:

Am Mittwoch, den 22.02.2006, 10:38 -0800 schrieb Rob K.:

Out of couriosity, how does this differ from act_as_nested_set?

A child can have multiple parents. All children under a specific parent
have a sort order. The same children under another parent may have a
different order.


Norman T.

http://blog.inlet-media.de


#9

In addition, I currently have this as my controller code that gets
triggered when the drop takes place (it’s probably way off and doesn’t
seem to do anything or trigger errors at the moment).

def order
params[:categoryartworks].each_with_index { |id,idx|
Category.find(params[:category]).artworks.find(id).insert_at(:position
=> idx) }
@category = Category.find(params[:category])
render :partial => “move_artwork”
end

Cheers,

Brendon

Brendon M. wrote:

I’m having real difficulty understanding how to use this plugin.

I have the following:

Artist has many artworks and categories
Categories and artworks have one artist

Categories have many artworks
Artworks belong to many categories
(a habtm relationship?)

The big deal here is that I want artworks to be ordered within their
specific categories though they may belong to many. (this is what your
plugin does) however, I can’t find any documentation that tells me how
to create that relationship to begin with. The user can make a bunch of
artworks, and make a bunch of categories. Then they can open a view that
shows the category box on the left and the list of available artworks on
the right and they can then drag and drop the artworks backward and
forward between the lists, and sort them within the categories list.

How exactly do I do this? as the moment I drop an artwork onto the
category it’s going to send the array of artworks in the category back
to the controller which then needs to run through them and update their
position in the join table somehow.

I’m a bit of a n00b and the lack of documentation has caught me off
guard here :slight_smile:

Cheers,

Brendon M.

Norman T. wrote:

Am Mittwoch, den 22.02.2006, 10:38 -0800 schrieb Rob K.:

Out of couriosity, how does this differ from act_as_nested_set?

A child can have multiple parents. All children under a specific parent
have a sort order. The same children under another parent may have a
different order.


Norman T.

http://blog.inlet-media.de


#10

Brendon,

I too have been struggling with this issue over the past week, and
this is the solution I found and so far it works great (== much
better) for me.

Translated into what I think you want/need

class Category < ActiveRecord::Base
has_many :artworks,
:through => :artwork_categories

get rid of all artwork categories when we delete a category

has_many :artwork_categories,
:dependent => true,
:order => :position
end
class Artwork < ActiveRecord::Base
belongs_to :artist
has_many :categories, :through => :artwork_categories

OK, make sure to get rid of any artwork_category entry

when we delete an artwork

has_many :artwork_categories, :dependent => true
end
class ArtworkCategory < ActiveRecord::Base
belongs_to :category
belongs_to :artwork
acts_as_list :scope => :category_id
end
class Artist < ActiveRecord::Base
has_many :artworks
end

db.structure

create_table :category do |t|
t.column :name, :string
end
create_table :artwork do |t|
t.column :artist_id, :integer
t.column :desc, :text
end
create_table :artwork_categories do |t|
t.column :artwork_id, :integer
t.column :category_id, :integer
t.column :position, :integer
end
create_table :artist do |t|
t.column :name, :string
end

The above should hopefully work as you want, if not all the way, then
at least some of the way. :wink: Also read through all the posts at
blog.hasmanythrough.com they are a goldmine !!

Another really good thing to work with as a n00b, is Zentest
[ http://nubyonrails.com/articles/2006/04/19/autotest-rails] for all
the info. It’s a really good thing to use when you’re trying to learn
how to do things :wink:

Hope that got you moving forward, apologies for the rushed nature of
this post, but short of time right now.

On 14 Jul 2006, at 05:58, Brendon M. wrote:

How exactly do I do this? as the moment I drop an artwork onto the

Norman T. wrote:

Am Mittwoch, den 22.02.2006, 10:38 -0800 schrieb Rob K.:

Out of couriosity, how does this differ from act_as_nested_set?

A child can have multiple parents. All children under a specific
parent
have a sort order. The same children under another parent may have a
different order.

Kind regards,

Mats


“TextMate, coding with an incredible sense of joy and ease”