ruby - Using a has_many association with a single table inheritance model in rails -


i'm trying create simple has_many association between game , dlc models. challenge i'm facing since there's no dlc table because of single table inheritance, there's no way insert game_id. if following i'd error:

game = game.create game.dlcs  sqlite3::sqlexception: no such column: games.game_id 

here how models setup:

class game < activerecord::base     has_many :dlcs end  class dlc < game     belongs_to :game end 

note: dlc refers downloadable content

the simplest alternative use self-joins , add parent_id column games.

class game < activerecord::base   has_many :dlcs unless self.name == 'dlc' end  class dlc < game   belongs_to :game, foreign_key: :parent_id end 

if that's absolutely unthinkable can create join table.

# game_id: int # dlc_id: int class gameextension   belongs_to :game   belongs_to :dlc end  class game < activerecord::base   has_many :game_extensions   has_many :dlcs, though: :gameextension end  class dlc < game   has_many :game_extensions   belongs_to :game, though: :gameextension end 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -