how to create relationship for polymorphic association
See original GitHub issuefor polymorphic association, how to create relationship.
class Picture < ApplicationRecord
belongs_to :imageable, polymorphic: true
end
class Employee < ApplicationRecord
has_many :pictures, as: :imageable
end
class Product < ApplicationRecord
has_many :pictures, as: :imageable
end
create_table :pictures do |t|
t.string :name
t.integer :imageable_id
t.string :imageable_type
t.timestamps
end
how to create relationship, so i can query like this
{
products {
name
pictures {
id
name
}
}
}
thanks !
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
4 Ways to Model Polymorphic Associations in Rails 5 - Medium
So, while you can't model a one to many relationship with a reverse relation, it can be a good option for a one...
Read more >Rails Techniques: Using Polymorphic Associations - Semaphore
In Ruby on Rails, a polymorphic association is an Active Record association that can connect a model to multiple other models.
Read more >Understanding Polymorphic Relationships - DevDojo
Adding polymorphic relationships can make your app more efficient, flexible, and easier to program. Digging Deeper. Polymorphic relationships ...
Read more >Modeling Polymorphic Associations in a Relational Database
Another approach is to use a join table for each relationship type. create table acl_document( acl_id integer not null unique ...
Read more >Polymorphic Associations - Sequelize
Define the junction model explicitly, specifying the two foreign keys as tagId and taggableId (this way it is a junction model for a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
@dfang @SkaterDad
We don’t natively support polymorphic associations like that of Rails, however you’ll still be able to query like you wanted. The
pictures
table will be something along these lines:You can create views as follows:
Now you can define a manual relationship called
pictures
onemployees
andproducts
table using these views. So your queries will now look like this:Another option is to use through table like @shahidhk suggested, i.e,
employee_pictures
,product_pictures
… etc. If you have such a modelling, Hasura makes it very easy to insert into these tables using relationships, i.e, you can insert a picture and associate it with an employee in a single query. If you have such a modelling, your queries will look like this:So it is up to you to pick a schema design that suits your use case. The GraphQL queries will roughly be the same.
Hi @0x777
Thank you for the solution, nice workaround with views 👍
But I do have an issue, after creating my new relationship the UI for data schema crash (JS error) with the message “Something went wrong”
If I remove the relationship directly in the database it works again. I’ve traced the JS error to this line: https://github.com/hasura/graphql-engine/blob/c3f4c35141f1187fc2f0f107158c1dff26f34568/console/src/components/Services/Data/TableRelationships/autoRelations.js#L110
Here is my faulty
hdb_relationship
row:Did I missed something or this is a bug? Thank you.