polymorphic relational associations (using has_many_related)

154 views
Skip to first unread message

Josef Richter

unread,
Jun 26, 2010, 10:05:58 AM6/26/10
to 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

unread,
Jul 16, 2010, 1:56:54 PM7/16/10
to 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
Reply all
Reply to author
Forward
0 new messages