Forum: Ruby on Rails has_many :through with a polymorphic join

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2006-05-21 16:15
(Received via mailing list)
Hi,
Four tables: users, user_counties, uk_counties and us_counties.

Each user has many counties, and each county has many users, so I
decided to make user_counties a polymorph, so it can have counties
from different countries (each country requires a completely different
set of tables with a completely different set of properties, that's
why there's one table for uk_counties and one for us_counties, with
more to follow when more countries are supported). This would require:

class User...
  has_many :counties, :through => user_counties #counties is the
imaginary table
end

class UserCounties
   belongs_to :counties, :polymorphic => true
end

Is this possible? If not, can anyone think of a way around it other
than adding another table into the chain? And (bonus question... not
required, just for fun) what would the reverse relationship look like
(ie, how would uk_counties relate to users)?
-Nathan
Josh S. (Guest)
on 2006-05-22 04:44
unknown wrote:
> Four tables: users, user_counties, uk_counties and us_counties.
>
> Each user has many counties, and each county has many users, so I
> decided to make user_counties a polymorph...

http://blog.hasmanythrough.com/articles/2006/04/03...

--
Josh S.
http://blog.hasmanythrough.com
unknown (Guest)
on 2006-05-22 06:49
(Received via mailing list)
Ah, thanks Josh. Nice to see that you'd already written an in-depth
answer to my problem.
-N
Josh S. (Guest)
on 2006-05-22 08:57
unknown wrote:
> Ah, thanks Josh. Nice to see that you'd already written an in-depth
> answer to my problem.

About half the reason I keep the blog is to avoid having to repeat
myself :-)
Glad you found it useful!

--
Josh S.
http://blog.hasmanythrough.com
This topic is locked and can not be replied to.