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
Post a Comment