So this is more or less of what I'm trying:
I have an abstract class extracted out for all the common functionalities, Which includes a polymorphic association, so it looks like this:
class Card < ActiveRecord::Base
self.abstract_class = true
belongs_to :cardable, polymorphic: true
...
end
class Spell < Card
...
end
class Unit < Card
...
end
Now when i try to use polymorphic associations on this, something on the lines of:
class Deck < ActiveRecord::Base
has_many :cards, :as => :cardable
end
class Hand < ActiveRecord::Base
has_many :cards, :as => :cardable
end
The belongs_to
part of things work fine,
i.e. spell.cardable
works perfectly as expected
However, because of the abstract class, has_many
doesn't work well
i.e. hand.cards
or deck.cards
always gives an empty ActiveRecord Association
Is this a workable model, or if not, what would be a better way to model this entire scenario?
@JoshKurien well it sounds like you've answered your own question "it does not need a corresponding table" but in your case aren't cards
an actual table in your DB? If so, just get rid of this line as it's clearly breaking the polymorphism provided by ActiveRecord
self.abstract_class = true
Perhaps a better way is to just let each model class which has a database table be defined as a regular model. If you need shared behavior in your models, can you use model concerns and extend those models from a shared concern?
See How to use concerns in Rails 4
But there are some who think concerns are a bad design, in that case you may also want to check out https://github.com/AndyObtiva/super_module as another alternative.