Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - acts_as_list with multiple Models

I've managed to use act_as_list with my Models (it was quite easy) one by one, but now i have a problem.

In my app there are 3 models: Facility, Service and Activity. I need to use acts_as_list on their union... is it possible to do that?

Hope my question is clear

like image 603
Abramodj Avatar asked Jun 13 '11 11:06

Abramodj


1 Answers

You should use a fourth model with a polymorphic association, then put the list on that.

First, read up on polymorphic associations to understand this: http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

Now you'll want to have a class that looks like this:

class Position < ActiveRecord::Base
  belongs_to :positionable, polymorphic: true
end

And a migration that looks like this:

class CreatePositions < ActiveRecord::Migration
  def change
    create_table :position do |t|
      t.integer :positionable_id
      t.string  :positionable_type
      t.timestamps
    end
  end
end

Then on each of the other models add this:

class Facility < ActiveRecord::Base
  has_one :position, as: :positionable
  # ...
end
like image 185
Jeff Dickey Avatar answered Sep 29 '22 10:09

Jeff Dickey