Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Polymorphic has_and_belongs_to_many association
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
ChuckE  
View profile  
 More options Sep 8 2012, 7:30 am
From: ChuckE <honeyryderch...@gmail.com>
Date: Sat, 8 Sep 2012 04:30:42 -0700 (PDT)
Local: Sat, Sep 8 2012 7:30 am
Subject: Polymorphic has_and_belongs_to_many association

Hi,

I'm currently having the situation where I have to implement a polymorphic
has_and_belongs_to_many by introducing an in between model that handles all
the "has_many :as" and "belongs_to :polymorphic" association
implementations... and is not doing anything else. Thing I don't like it
here is, I am unnecessarily loading AR instances only to execute SQL that
leads me to whatever data I want to fetch. I would then gladly remove the
models, keep the DB tables, and have is all work with some kind of
has_and_belongs_to_many polymorphic magic. Something like:

class Song < ActiveRecord::Base
  has_and_belongs_to_many :genres, :as => :taggableend
class Genre < ActiveRecord::Base
  has_and_belongs_to_many :taggables, :polymorphic => trueend

or similar, I haven't put much thought into that. I would like to hear some
opinion of more experienced Rails developers to know if this would be a
valuable feature.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steve Klabnik  
View profile  
 More options Sep 10 2012, 10:20 am
From: Steve Klabnik <st...@steveklabnik.com>
Date: Mon, 10 Sep 2012 09:20:00 -0500
Subject: Re: [Rails-core] Polymorphic has_and_belongs_to_many association

Habtm is only good for simple relationships; you'll want to use has_many
:through, which I believe works just fine with polymorphism.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josh Susser  
View profile  
 More options Sep 10 2012, 11:02 am
From: Josh Susser <j...@hasmanythrough.com>
Date: Mon, 10 Sep 2012 08:02:07 -0700
Local: Mon, Sep 10 2012 11:02 am
Subject: Re: [Rails-core] Polymorphic has_and_belongs_to_many association

What you are describing cannot be accomplished with a single query. The issue is that a polymorphic association requires that you query the db to determine the class of the model, and you need that information to know what table to query to get the record you want. Some SQL dbs provide some kind of recursive query that might be able to do what you want in one query, but it would be pretty complicated and I can't guess how difficult it would be to manage the performance of those queries. That doesn't seem like something for core. If you want many-to-many association with polymorphism in one query, one way is to use has_many :through with an STI associated model.

--
Josh Susser


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Godfrey Chan  
View profile  
 More options Sep 10 2012, 1:34 pm
From: Godfrey Chan <godfrey...@gmail.com>
Date: Mon, 10 Sep 2012 10:34:07 -0700
Local: Mon, Sep 10 2012 1:34 pm
Subject: Re: [Rails-core] Polymorphic has_and_belongs_to_many association

On 2012-09-10, at 8:02 AM, Josh Susser wrote:

> The issue is that a polymorphic association requires that you query the db to determine the class of the model, and you need that information to know what table to query to get the record you want. Some SQL dbs provide some kind of recursive query that might be able to do what you want in one query, but it would be pretty complicated and I can't guess how difficult it would be to manage the performance of those queries.

Actually, since we are only storing the model name in the DB, it simply cannot be done at all, as the model name might be different from the table name.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »