On polymorphic associations with has_many through, is it possible to pull in multiple source types with one query?

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

On polymorphic associations with has_many through, is it possible to pull in multiple source types with one query?

Blake LeBlanc
When dealing with polymorphic associations, is it possible to have a has_many through query that pulls in ALL available source_types?

My understanding so far is that each source type needs its own query method, as I show here in the Image model

**image.rb**
```
has_many :image_tags
has_many :tags, through: :image_tags, source: :taggable, source_type: 'Tag'
has_many :people, through: :image_tags, source: :taggable, source_type: 'Person'
has_many :businesses, through: :image_tags, source: :taggable, source_type: 'Business'
...
```

**tag.rb**
```
has_many :image_tags, as: :taggable
has_many :images, through: :image_tags
```

**image_tag.rb**

```
belongs_to :image
belongs_to :taggable, polymorphic: true

def build_taggable(params)
  self.taggable = taggable_type.constantize.new(params)
end
```

What I'd like to be able to do, however, is create one query method that pulls in all associated records regardless of what source_type they might belong to.

Just thinking out loud, would that likely involve creating some sort of raw SQL join that acts directly on the ImageTags table? Or is there a more Railsy/ActiveRecordy way of approaching it? 

Thank you in advance for any help or insights!
-Blake

--
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/c537e34b-2d49-4e30-a197-2668fb773f99%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: On polymorphic associations with has_many through, is it possible to pull in multiple source types with one query?

Ariel Juodziukynas
I'm not sure if you can have all that with a single AR query, I think you need a custom query (I would use a UNION query an I think AR doesn't support that)

El jue., 19 mar. 2020 a las 13:25, Blake LeBlanc (<[hidden email]>) escribió:
When dealing with polymorphic associations, is it possible to have a has_many through query that pulls in ALL available source_types?

My understanding so far is that each source type needs its own query method, as I show here in the Image model

**image.rb**
```
has_many :image_tags
has_many :tags, through: :image_tags, source: :taggable, source_type: 'Tag'
has_many :people, through: :image_tags, source: :taggable, source_type: 'Person'
has_many :businesses, through: :image_tags, source: :taggable, source_type: 'Business'
...
```

**tag.rb**
```
has_many :image_tags, as: :taggable
has_many :images, through: :image_tags
```

**image_tag.rb**

```
belongs_to :image
belongs_to :taggable, polymorphic: true

def build_taggable(params)
  self.taggable = taggable_type.constantize.new(params)
end
```

What I'd like to be able to do, however, is create one query method that pulls in all associated records regardless of what source_type they might belong to.

Just thinking out loud, would that likely involve creating some sort of raw SQL join that acts directly on the ImageTags table? Or is there a more Railsy/ActiveRecordy way of approaching it? 

Thank you in advance for any help or insights!
-Blake

--
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/c537e34b-2d49-4e30-a197-2668fb773f99%40googlegroups.com.

--
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/CAPS3bcBnGfBU4xB_yc%3DBdKG8zEpZ-UKKwi2td68qYgkLPP-xhw%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: On polymorphic associations with has_many through, is it possible to pull in multiple source types with one query?

Blake LeBlanc
Thank you Ariel, I'll look into it! Because right now I'm running each source_type query individually then concat-ing the results into a single array. Gets the job done, but it'd be nice to tackle it all in one fell swoop.

On Thursday, March 19, 2020 at 5:20:33 PM UTC-5, Ariel Juodziukynas wrote:
I'm not sure if you can have all that with a single AR query, I think you need a custom query (I would use a UNION query an I think AR doesn't support that)

El jue., 19 mar. 2020 a las 13:25, Blake LeBlanc (<<a href="javascript:" target="_blank" gdf-obfuscated-mailto="HIUD6s3RBwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">blake....@...>) escribió:
When dealing with polymorphic associations, is it possible to have a has_many through query that pulls in ALL available source_types?

My understanding so far is that each source type needs its own query method, as I show here in the Image model

**image.rb**
```
has_many :image_tags
has_many :tags, through: :image_tags, source: :taggable, source_type: 'Tag'
has_many :people, through: :image_tags, source: :taggable, source_type: 'Person'
has_many :businesses, through: :image_tags, source: :taggable, source_type: 'Business'
...
```

**tag.rb**
```
has_many :image_tags, as: :taggable
has_many :images, through: :image_tags
```

**image_tag.rb**

```
belongs_to :image
belongs_to :taggable, polymorphic: true

def build_taggable(params)
  self.taggable = taggable_type.constantize.new(params)
end
```

What I'd like to be able to do, however, is create one query method that pulls in all associated records regardless of what source_type they might belong to.

Just thinking out loud, would that likely involve creating some sort of raw SQL join that acts directly on the ImageTags table? Or is there a more Railsy/ActiveRecordy way of approaching it? 

Thank you in advance for any help or insights!
-Blake

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="HIUD6s3RBwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/c537e34b-2d49-4e30-a197-2668fb773f99%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/c537e34b-2d49-4e30-a197-2668fb773f99%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/c537e34b-2d49-4e30-a197-2668fb773f99%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/c537e34b-2d49-4e30-a197-2668fb773f99%40googlegroups.com.

--
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/bfc6e653-b594-411f-91c6-676b7dc4f9e9%40googlegroups.com.