polymorphic relational associations (using has_many_related)

155 visningar
Hoppa till det första olästa meddelandet

Josef Richter

oläst,
26 juni 2010 10:05:582010-06-26
till Mongoid
Hi,

I have Suggestion, Feature and Vote models.
suggestion embeds_many features
suggestion has_many_related votes
feature has_many_related votes

I am trying to make those has_many_related associations polymorphic.
but I had no luck using options like :polymorphic => true and
has_many_related :votes, :as => :votable

here is the gist with relevant code http://gist.github.com/454061

as you can see, for the time being I used belongs_to twice in Vote
model, which puts suggestion_id and feature_id fields into each vote,
where I use only one of them. I also have a conditional statement in
votes_controller which recognizes whether I am casting vote on
suggestion or feature.

but this is obviously not true polymorphism. could you please give me
some hints what am I doing wrong and what is the right way, please?

Thanks very much

Josef

nader

oläst,
16 juli 2010 13:56:542010-07-16
till Mongoid
does anyone know what the state of polymorpic associations is? the
documentation says it's there only for embedded docs.

On Jun 26, 7:05 am, Josef Richter <richter.jo...@gmail.com> wrote:
> Hi,
>
> I have Suggestion, Feature and Vote models.
> suggestion embeds_many features
> suggestion has_many_related votes
> feature has_many_related votes
>
> I am trying to make those has_many_related associationspolymorphic.
> but I had no luck using options like :polymorphic=> true and
> has_many_related :votes, :as => :votable
>
> here is the gist with relevant codehttp://gist.github.com/454061
Svara alla
Svara författaren
Vidarebefordra
0 nya meddelanden