Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing activerecord and maintain subclass after db fetch

I have an ActiveRecord model Media which is supposed to be able to store similarly structured information about different types of media (Media::Book, Media::Movie, Media::Music). However each of these subclasses has unique methods.

# TABLE medias
# string :title
# string :description
# integer :media_type

class Media < ActiveRecord::Base
end

class Media
  class Book < Media
    def reviews
      GoogleBooks.search(name).get_reviews
    end
  end
  class Movie < Media
    def reviews
      IMDB.search_movies(name).reviews
    end
  end
  class Music < Media
    def reviews
      Lastfm.search(name).comments
    end

    def music_video
      Youtube.search(name).first.embed_html
    end
  end
end

This would work if I used Media::Book.new("Harry Potter").reviews, but I want to be able to use

Media.find("Harry Potter")
=> Media::Book

and

Media.find("Harry Potter").reviews

I know I should use media_type to achieve this, but I'm not sure if theres a better way to do it than overriding every single ActiveRecord database interface method (User.medias, find, find_by_etc, where, order, limit, all) and replacing each of their return values.

like image 229
user545139 Avatar asked Feb 23 '23 16:02

user545139


1 Answers

you can use ActiveRecords Single Table Inheritance feature for that. It uses another column on your models table (defaults to a column named "type") to determine the type of the model for every record.

Just add a string type column to your medias table and Rails will do the magic for you when it finds that column in your databases schema. If you are fine with AR adding the type to your media_type column, you can also change the column used for Single Table Inheritance using the set_inheritance_column class method.

class Media < ActiveRecord::Base
  set_inheritance_column :media_type
end

Then you'll find the class name of the corresponding object with its full namespaced classname in this column. (eg.: "Media::Book") That said, you don't wanna change the content of the type column (or whatever column you use) manually. ActiveRecord will take care of it by itself.

Have a look at http://api.rubyonrails.org/classes/ActiveRecord/Base.html and search for "Single table inheritance" within that page for further information

Edit: just realized your media_type column is an integer. So using it for STI won't work. Just stick to a string type column and let AR do the rest for you.

like image 87
Pascal Avatar answered Mar 08 '23 17:03

Pascal