Skip to content

Instantly share code, notes, and snippets.

@jcoglan
Created April 15, 2014 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcoglan/10758137 to your computer and use it in GitHub Desktop.
Save jcoglan/10758137 to your computer and use it in GitHub Desktop.
# Migrate from this:
class ListItem < ActiveRecord::Base
belongs_to :group
acts_as_list scope: :group
# has id and type columns
end
class Audio < ListItem
end
class Video < ListItem
end
# to this, where parts are temporary measures to support the new models on top of the legacy DB:
class ListItem < ActiveRecord::Base
belongs_to :content, polymorphic: true, foreign_key: :id, foreign_type: :type
belongs_to :group
acts_as_list scope: :group
def self.inheritance_column
:no_such_column_because_type_no_longer_references_subclasses_of_ListItem
end
end
class Audio < ActiveRecord::Base
self.table_name = :list_items
has_many :list_items, as: :content, foreign_key: :id, foreign_type: :type
end
class Video < ActiveRecord::Base
self.table_name = :list_items
has_many :list_items, as: :content, foreign_key: :id, foreign_type: :type
end
@pixeltrix
Copy link

Am I just being dense here but you seem to be wanting to do a query like this for the list_items association:

SELECT list_items.* FROM list_items
WHERE list_items.id = 1 AND list_items.type = 'Video'

Isn't this only ever going to return itself as a ListItem? Is the intention there will be more than one list item in the future? Also there's a problem that :foreign_type will be suffixed with _type so it's not going to work against the default :type column.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment