Setting model association `source_type` to polymorphic object

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
UG
Reply | Threaded
Open this post in threaded view
|

Setting model association `source_type` to polymorphic object

UG
I am trying to setup a way for my `User` and `Project` model to both be followed through the same `Relationship` model.

I also want to be able to get all items the user has followed with this association:
`has_many :following, through: :active_relationships, source: :followable, source_type: "Followable"`


This way, the projects and users any user has followed can be called with `user.following`. In this case, `Followable` is the polymorphic object that is attributed to both users and projects. Unfortunately, it seems that I cannot set my source_type to a polymorphic object. Is there a way I can bypass this?

Many thanks


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/fea19464-bc13-4d28-a316-b253328d1fdd%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Setting model association `source_type` to polymorphic object

Jason Fleetwood-Boldt
I don't think polymorphism in Rails can work that way 

did you try


`has_many :users_following, through: :active_relationships, source: :followable, source_type: "FollowableUser"`

`has_many :projects_following, through: :active_relationships, source: :followable, source_type: "FollowableProject"`




On Monday, February 10, 2020 at 5:45:41 AM UTC-5, UG wrote:
I am trying to setup a way for my `User` and `Project` model to both be followed through the same `Relationship` model.

I also want to be able to get all items the user has followed with this association:
`has_many :following, through: :active_relationships, source: :followable, source_type: "Followable"`


This way, the projects and users any user has followed can be called with `user.following`. In this case, `Followable` is the polymorphic object that is attributed to both users and projects. Unfortunately, it seems that I cannot set my source_type to a polymorphic object. Is there a way I can bypass this?

Many thanks


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0facc107-25e3-4eea-9bae-753e5488ed7e%40googlegroups.com.
UG
Reply | Threaded
Open this post in threaded view
|

Re: Setting model association `source_type` to polymorphic object

UG


On Tuesday, February 18, 2020 at 6:59:15 PM UTC+1, Momeas Interactive wrote:
I don't think polymorphism in Rails can work that way 

did you try


`has_many :users_following, through: :active_relationships, source: :followable, source_type: "FollowableUser"`

`has_many :projects_following, through: :active_relationships, source: :followable, source_type: "FollowableProject"`



Yeah I guess that would work. I would need to change a lot in my code but yeah I could do that. It's too bad if you can't do it in Rails

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b3df4b44-223e-42aa-a2d2-737a86cc09a9%40googlegroups.com.