Can I use acts_as_list with a has_many :through association


#1

I’d like to be able to use a has_many :through association and treat
the associations as a list but I’m getting this error when I try an
use an acts_as_list method:

NoMethodError: undefined method `move_to_bottom’

I’m using edge rails r6786.

Here are my domain rules:

Activities are things students can do.
Units consists of a sequenced list of Activities.
Activities can be used in many Units.

Here are the associations for these rules:

class Activity < ActiveRecord::Base
has_many :unit_activities
has_many :units, :through => :unit_activities
end

class Unit < ActiveRecord::Base
has_many :unit_activities, :order => :position
has_many :activities, :through => :unit_activities
end

class UnitActivity < ActiveRecord::Base
belongs_to :activity
belongs_to :unit
acts_as_list :scope => :unit_id
end

In he example below in script/console I load in some previously
created activities; make a new unit; and add the activities to the
unit.

activity_crystals = Activity.find_by_name(“Molecular crystals”)
activity_browning = Activity.find_by_name(“Brownian motion”)
activity_mixing = Activity.find_by_name(“Temperature of mixing
water - TEEMSS demo”)
my_unit = Unit.create(:name => “My First Unit”, :description => “A
simple test unit to see if thenew associations work”)

my_unit.activities.push(activity_crystals, activity_browning,
activity_mixing)

my_unit.activities.each { |a| puts “#{a.id}: #{a.name}” }; nil
55: Molecular crystals
42: Brownian motion
25: Temperature of mixing water - TEEMSS demo

Here’s where the error occurs:

my_unit.activities[0].move_to_bottom
NoMethodError: undefined method `move_to_bottom’ for
#Activity:0x34dec54
from

At this point my unit_activities table looks like this:

id activity_id unit_id position

1 55 1 1
2 42 1 2
3 25 1 3


#2

my_unit.activities accesses the items of the Activities table, which
are not marked as acts_as_list
The unit_activities items are enabled as acts_as_list.
Unit -> unit_activites -------> activties
(acts_as_list) (NOT acts_as_list)

by adding these activities to your association, a new unit_activities
item was created for each activity. and those are acts_as_list

try this:

my_unit.unit_activities[0].move_to_bottom

On 22 Mai, 08:30, Stephen B. removed_email_address@domain.invalid


#3

my_unit.unit_activities[0].move_to_bottom

Thanks for taking the time to write Thorsten. That makes everything MUCH
more clear!


#4

Had problems with this too. Something you might want to have a look at
is this:
http://euphemize.net/blog/archives/2007/06/04/learning-rails-many-to-many-relationships/

Finally got things working.

On May 22, 10:42 pm, Stephen B. removed_email_address@domain.invalid