# Acts\_as\_list :scope with habtm

**URL:** <https://www.ruby-forum.com/t/acts-as-list-scope-with-habtm/109691>\
**Category:** Rails\
**Created:** [July 17, 2007, 11:34pm UTC](https://www.ruby-forum.com/t/acts-as-list-scope-with-habtm/109691 "2007-07-17T23:34:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@martin](https://www.ruby-forum.com/u/martin)\
**Post date:** [July 17, 2007, 11:34pm UTC](https://www.ruby-forum.com/t/acts-as-list-scope-with-habtm/109691/1 "2007-07-17T23:34:38Z")

</div>

Hello,

I have a problem with a little more complex DB-schema.

My target are and sorted trees like this:  
1 One  
1.1 One.One  
1.2 One.Two  
2 Two

I use for it two models:

- top: This an item, with the name and position
- agenda: Collections of tops

class Agenda \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :tops  
end

class Top \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :agendas  
acts\_as\_list :scope =\> :parent\_id  
acts\_as\_tree :order =\> :position  
end

Datebase tops:  
id | parent\_id | position | title  
1 | 0 | 1 | One  
2 | 0 | 2 | Two  
3 | 1 | 1 | One.One  
4 | 1 | 2 | One.Two

Looks like: [valibuk.net » One Model With acts\_as\_list and acts\_as\_tree](http://www.valibuk.net/?p=42)

Works fine.

But my problem: The tops-database include tops for all agendas. So a  
second agenda need also a new top with parent\_id=0 and position=1:  
5 | 0 | 1 | New One  
… but acts\_as\_list used (sadly) correctly parent\_id=0 and position=3

Result:  
acts\_as\_list :scope =\> :parent\_id + “correct agendas”

Which scope-option I need? Anyone can me help with the SQL-statement?

Or I must used acts\_as\_nested\_set? Never used it. But there the same  
problem with :scope and habtm-relationship, I think.

Thanks for help  
Martin

---

<div class="post-metadata">

**Author:** ![martin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@martin](https://www.ruby-forum.com/u/martin)\
**Post date:** [July 20, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/acts-as-list-scope-with-habtm/109691/2 "2007-07-20T19:40:52Z")

</div>

Martin schrieb:

> I have a problem with a little more complex DB-schema.

Anybody an idea?
