# Acts\_as\_list with has\_and\_belongs\_to\_many

**URL:** <https://www.ruby-forum.com/t/acts-as-list-with-has-and-belongs-to-many/50073>\
**Category:** Rails\
**Created:** [November 18, 2005, 10:12pm UTC](https://www.ruby-forum.com/t/acts-as-list-with-has-and-belongs-to-many/50073 "2005-11-18T22:12:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 18, 2005, 10:12pm UTC](https://www.ruby-forum.com/t/acts-as-list-with-has-and-belongs-to-many/50073/1 "2005-11-18T22:12:24Z")

</div>

Hi,  
I found the problem below on the Rails wiki and have the very same  
problem.  
Can anyone suggest a solution that does not involve an extra object like  
on  
the wiki?  
Thanks,  
Peter  
From: [Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowtoUseActsAsList)  
I’m looking for a Howto that would explain how to use  
ActsAsList[http://wiki.rubyonrails.com/rails/pages/ActsAsList](http://wiki.rubyonrails.com/rails/pages/ActsAsList)where  
you have something like the following:

Book

- id
- name

Category

- id
- name

books\_categories

- book\_id
- category\_id
- position

The position in the books\_categories table is relative to the category.  
Thus, under one category book A might be in position 4, while in another  
category it might be in position 1.

---

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 19, 2005, 12:01am UTC](https://www.ruby-forum.com/t/acts-as-list-with-has-and-belongs-to-many/50073/2 "2005-11-19T00:01:21Z")

</div>

Ok, I’ve searched everywhere trying to figure this one out… Mechanize  
(installed from gems) is trying to reference a function that it can’t  
find.

I’m using:

- Mechanize (install gem mechanize)
- the newest Ruby / Rails versions.

I’m getting this error:  
undefined method `add\_header’ for #\<Net::HTTP::Post POST\>

I know that mechanize is doing something with some files from Ruby 1.9  
and  
has a file that says this:

# required due to the missing get\_fields method in Ruby 1.8.2

$LOAD\_PATH.unshift File.join(File.dirname( **FILE** ), “mechanize”,  
“net-overrides”)  
and that it has this file:

/usr/local/lib/ruby/gems/1.8/gems/mechanize-0.3.1/lib/mechanize/net-override  
s/net/http.rb

So, for some reason it would appear that mechanize can’t see the  
over-ridden  
function???

It had the same problem with the “get\_fields” function (which is in the  
same  
place, and is a thing the mechanize developer added since Ruby 1.8  
doesn’t  
have the response.get\_fields function), but I got around that by adding  
a  
function:  
def get\_fields2(response,key)  
return nil unless response.header[key.downcase]  
response.header[key.downcase].dup  
end  
to /usr/local/lib/ruby/gems/1.8/gems/mechanize-0.3.1/lib/mechanize.rb  
and  
then changing the references to “response.get\_fields(…)” to  
“get\_fields2(response,…)” … But that approach doesn’t work for the  
add\_header function.

Thanks for any help!  
-Greg

Greg E.  
Chief Technical Officer  
Eyetools, Inc.  
(916) 792-4538  
removed\_email\_address@domain.invalid  
[http://www.eyetools.com](http://www.eyetools.com)

-----Original Message-----  
From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]On Behalf Of Peter M.  
Sent: Friday, November 18, 2005 1:10 PM  
To: removed\_email\_address@domain.invalid  
Subject: [Rails] acts\_as\_list with has\_and\_belongs\_to\_many

Hi,

I found the problem below on the Rails wiki and have the very same  
problem. Can anyone suggest a solution that does not involve an extra  
object  
like on the wiki?

Thanks,  
Peter

From: [Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowtoUseActsAsList)

I’m looking for a Howto that would explain how to use ActsAsList where  
you  
have something like the following:  
Book

- id
- name

Category

- id
- name

books\_categories

- book\_id
- category\_id
- position  
The position in the books\_categories table is relative to the category.  
Thus, under one category book A might be in position 4, while in another  
category it might be in position 1.

---

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 19, 2005, 1:53am UTC](https://www.ruby-forum.com/t/acts-as-list-with-has-and-belongs-to-many/50073/3 "2005-11-19T01:53:05Z")

</div>

Reading the Rails source for acts\_as\_list and the ActiveRecord base.rb,  
part  
of the problem seems to be that acts\_as\_list doesn’t now about model  
associations. Also it appears that sql update statements made with rails  
update\_all cannot have multiple tables. Is all of this true?

Couldn’t we have something like the following with the conventional join  
table being included automatically?

Class Book \< ActiveRecord::Base

has\_and\_belongs\_to\_many :categories

acts\_as\_list :include =\> :category,  
:scope =\> :category\_id

end

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 24, 2005, 3:56am UTC](https://www.ruby-forum.com/t/acts-as-list-with-has-and-belongs-to-many/50073/4 "2005-11-24T03:56:09Z")

</div>

Peter M. \<petermichaux@…\> writes:

> Hi,
> 
> I found the problem below on the Rails wiki and have the very same problem.  
> Can anyone suggest a solution that does not involve an extra object like  
> on the  
> wiki?
> 
> Thanks,  
> Peter
> 
> From: [Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowtoUseActsAsList)
> 
> I’m looking for a Howto that would explain how to use ActsAsList where you  
> have something like the following:

Hi,

I just struggled through this myself, and came up with a temporary, but  
relatively simple solution:

[http://wiki.rubyonrails.com/rails/pages/HowToUseActsAsListWithHasAndBelongsToMany](http://wiki.rubyonrails.com/rails/pages/HowToUseActsAsListWithHasAndBelongsToMany)

I’m fairly new to rails and ruby, though not DBs & Web programming, so  
feel free  
to refine the technique where it could be made more elegant!
